Rules: how to avoid PWM > 1023 ?

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
costo
Normal user
Posts: 490
Joined: 21 Nov 2015, 15:03

Rules: how to avoid PWM > 1023 ?

#1 Post by costo » 19 Jul 2019, 03:08

Hi,

I use ESPEasy version 148 talking to a RasPi/Domoticz server with MQTT.
A DS18B20 mounted on a heatsink measures temperature. GPIO-0 with PWM controls a fan that lowers the temp of the heatsink.
The fan should start at about 35C with a minimum PWM value of about 400 and above 75C PWM should always be 1023

Task 1 is a DS18b20 Named DS18 ValueName Temp
Task 4 Dummy Named Fan and ValueName PWM

I wrote this rule:

Code: Select all

On System#Boot do
  timerSet,1,15
endon
On Rules#Timer=1 do
  if [DS18#Temp] >35
     TaskValueSet 4,1,[DS18#Temp]*12
     PWM,0,[Fan#PWM]
  else
     TaskValueSet 4,1,0
     PWM,0,0
  endif
  timerSet,1,15
endon
The problem is that at above 85C the calculated PWM is >1023 making the fan stop, which should not happen.

What is the solution?

User avatar
ThomasB
Normal user
Posts: 400
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Rules: how to avoid PWM > 1023 ?

#2 Post by ThomasB » 19 Jul 2019, 17:45

The problem is that at above 85C the calculated PWM is >1023 making the fan stop, which should not happen. What is the solution?
Change this:

Code: Select all

PWM,0,[Fan#PWM]
To:

Code: Select all

if [Fan#PWM]<1023
 PWM,0,[Fan#PWM]
else
 PWM,0,1023
endif
This code is a nested if/then. I don't believe that old ESPEz versions allowed nesting in rules, so be sure to use a recent MEGA release.

- Thomas

costo
Normal user
Posts: 490
Joined: 21 Nov 2015, 15:03

Re: Rules: how to avoid PWM > 1023 ?

#3 Post by costo » 19 Jul 2019, 21:21

Thomas , thank you for the reply.

As you say, it is a nested it/then/else. As I am using the old V148 this will not work.
I am a bit reluctant to use the mega version. I experience a lot of restarts with the Mega software.
I prefer using the old software so have to find a different solution.
I think a second On/Endon part will do the trick but I am not sure how to define the trigger for a second On/endon.

User avatar
ThomasB
Normal user
Posts: 400
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Rules: how to avoid PWM > 1023 ?

#4 Post by ThomasB » 19 Jul 2019, 21:43

Does your old version support the Dummy Device plugin? If it does then you can copy the PWM value and limit it to 1023. Use the dummy value in your PWM statement.

As an alternative, you could use the "event" command to handle the PWM action. The event subroutine could limit the PWM value using if/then as shown in my previous post.

- Thomas

costo
Normal user
Posts: 490
Joined: 21 Nov 2015, 15:03

Re: Rules: how to avoid PWM > 1023 ?

#5 Post by costo » 20 Jul 2019, 00:23

Yes I use a dummu device at task 4
I extended the rule with another On/endon which should be triggered at a valuechange of [Fan#PWM]

Code: Select all

On System#Boot do
  timerSet,1,15
endon

On Rules#Timer=1 do
  if [DS18#Temp] >35
    TaskValueSet 4,1,[DS18#Temp]*12
    //PWM,0,[Fan#PWM]
  else                         
    TaskValueSet 4,1,0
    //PWM,0,0
  endif
  timerSet,1,15
endon

On [Fan#PWM] do
   if [Fan#PWM]>1023
      [Fan#PWM] = 1023
   endif
   PWM,0,[Fan#PWM]
endon
I commented out the PWM,0,x lines and added one in the last part but this does not work, the trigger [Fan#PWM] seems to not do anything because the fan is not turning. So the syntax is wrong I believe

User avatar
ThomasB
Normal user
Posts: 400
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Rules: how to avoid PWM > 1023 ?

#6 Post by ThomasB » 20 Jul 2019, 01:29

This example uses a dummy device at Task #6. Device name FanVar, Value name (1) is PWMval. Feel free to change them for your installation.

Code: Select all

On System#Boot do
  timerSet,1,15
endon

On Rules#Timer=1 do
   if [Fan#PWM]>1023
     TaskValueSet 6,1,1023  // Assign max value to FanVar#PWMval.
  else
     TaskValueSet 6,1,[Fan#PWM]  // Copy PWM to FanVar#PWMval.
  endif

  if [DS18#Temp] >35
     TaskValueSet 4,1,[DS18#Temp]*12
     PWM,0,[FanVar#PWMval]
  else
     TaskValueSet 4,1,0
     PWM,0,0
  endif
  timerSet,1,15
endon
- Thomas

costo
Normal user
Posts: 490
Joined: 21 Nov 2015, 15:03

Re: Rules: how to avoid PWM > 1023 ?

#7 Post by costo » 20 Jul 2019, 14:12

Thank you again for the response.

As far as I understand the rules for the old ESPEasy version, it is not possible/advisable/stable to use more than one if/else/enif statement in a On/EndOn rule.
So I had some doubts if it would work So I made a second dummy, not on task6 but on task 3 and tested it.
And it actually works.
So I will see if this rules is stable in the long run.

edit:

This is a little better, only one dummy device , Fan, on task 4 with two variables, PWMset and PWMval.

Code: Select all


On System#Boot do
  timerSet,1,15
endon

On Rules#Timer=1 do
   if [Fan#PWMset]>1023
     TaskValueSet 4,2,1023  // Assign max value to Fan#PWMval.
  else
     TaskValueSet 4,2,[Fan#PWMset]  // Copy PWMset to Fan#PWMval.
  endif

  if [DS18#Temp] >30
     TaskValueSet 4,1,[DS18#Temp]*12  // Calculate PWMset
     PWM,0,[Fan#PWMval]
  else
     TaskValueSet 4,1,0
     PWM,0,0
  endif
  timerSet,1,15
endon

User avatar
ThomasB
Normal user
Posts: 400
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Rules: how to avoid PWM > 1023 ?

#8 Post by ThomasB » 20 Jul 2019, 18:24

Glad to hear you solved the problem.

- Thomas

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 19 guests