TaskValueSet

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
abhelou
Normal user
Posts: 19
Joined: 22 Apr 2018, 21:29

TaskValueSet

#1 Post by abhelou » 08 Mar 2023, 23:17

hii
i have been doing this room controller for dimming the light. i made an AC dimmer which can be used using pwm.
toggle switch 0 ->pwm = 1023 -> turn off lamp
toggle switch 1 -> pwm = 0 -> turn on lamp full brightness
rotary encoder to control pwm
now my issue is when switch is 0, and i turn modify pwm using encoder, and i click on switch to adjust pwm to 1023 by one click to turn off, it will go to 1, so i made a rule if switch value is 1 and pwm isn't 1023, to TaskValueSet pwm to 1023 and TaskValueSet switch to 0. but this isn't saving the state in plugin, as it is turning off, but i have to toggle 2 times using switch button to turn on the lamp again.

any idea how to solve this?

rule used:
on sw#state do
if %eventvalue%=0 and [dummy#pwm]<1023
TaskValueSet 3,1,1023
endif

if %eventvalue%=1 and [dummy#pwm]=1023
TaskValueSet 3,1,0
else
TaskValueSet 3,1,1023
TaskValueSet 1,1,0
endif
endon

on rotary#counter do
TaskValueSet 3,1,1023-(%eventvalue%*10.23)
endif
rotary#counter=%eventvalue%
endon

on dummy#pwm do
PWM,2,%eventvalue%
endon
Attachments
1.png
1.png (30.78 KiB) Viewed 1824 times

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: TaskValueSet

#2 Post by TD-er » 08 Mar 2023, 23:25

Have you checked the checkbox on the Tools->Advanced page for "Allow TaskValueSet on all plugins" ?

abhelou
Normal user
Posts: 19
Joined: 22 Apr 2018, 21:29

Re: TaskValueSet

#3 Post by abhelou » 08 Mar 2023, 23:42

TD-er wrote: 08 Mar 2023, 23:25 Have you checked the checkbox on the Tools->Advanced page for "Allow TaskValueSet on all plugins" ?
yes it is enabled. on devices page, i can see state turn to 0, and light turn off, but when i click the switch again, it seems like it is still 1, but is shows 0. so i have to click once again to be able to toggle state to 1

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: TaskValueSet

#4 Post by Ath » 09 Mar 2023, 08:32

The Switch is checking the current level of the GPIO pin, not the task state, and with TaskValueSet you only change that state.
Instead of changing the state using TaskValueSet, you could set the level of the GPIO pin, using the GPIO,0,1 command or toggle the level by using GPIOToggle,0
/Ton (PayPal.me)

abhelou
Normal user
Posts: 19
Joined: 22 Apr 2018, 21:29

Re: TaskValueSet

#5 Post by abhelou » 09 Mar 2023, 17:10

Ath wrote: 09 Mar 2023, 08:32 The Switch is checking the current level of the GPIO pin, not the task state, and with TaskValueSet you only change that state.
Instead of changing the state using TaskValueSet, you could set the level of the GPIO pin, using the GPIO,0,1 command or toggle the level by using GPIOToggle,0
that doesn't work. the case is when switch state is 0, lamp is of, and i turned it on using the rotary encoder, and then i want to turn off without dimming to 0 brightness using the rotary encoder, but turn off using the switch.
so i made a rule when the switch change to 1, and brightness isn't zero, do not illuminate at full brightness, but turn off to brightness zero. it is working using the rule in the photo. but then if i click on switch later, i need to click it twice to be able to get back to state 1 full brightness

User avatar
chromo23
Normal user
Posts: 821
Joined: 10 Sep 2020, 16:02
Location: germany

Re: TaskValueSet

#6 Post by chromo23 » 09 Mar 2023, 17:38

You have a mistake in your rules

Code: Select all

on rotary#counter do
TaskValueSet 3,1,1023-(%eventvalue%*10.23)
endif  <---------------------------------------- if there is an "endif" there should be an "if" or no "endif" at all
rotary#counter=%eventvalue% <----------- what is this for?
endon
Edit: I personally find it hard to understand what you want to achieve. Maybe you can explain it for me a bit simpler.
Last edited by chromo23 on 09 Mar 2023, 17:58, edited 2 times in total.

abhelou
Normal user
Posts: 19
Joined: 22 Apr 2018, 21:29

Re: TaskValueSet

#7 Post by abhelou » 09 Mar 2023, 17:56

chromo23 wrote: 09 Mar 2023, 17:38 You have a mistake in your rules

Code: Select all



on rotary#counter do
TaskValueSet 3,1,1023-(%eventvalue%*10.23)
endif  <---------------------------------------- if there is an "endif" there should be an "if" or no "endif" at all
rotary#counter=%eventvalue% <----------- what is this for?
endon
yes there was an if which was omited and forgeot the endif, rotary#counter=%eventvalue% removed. actually these lines wasn't making any additional acts, but thanks i removed it. still same issue, after toggling switch to 0 if switch turn to 1 with condition, it needs to be clicked to twice to turn state to 1

User avatar
chromo23
Normal user
Posts: 821
Joined: 10 Sep 2020, 16:02
Location: germany

Re: TaskValueSet

#8 Post by chromo23 » 09 Mar 2023, 17:57

chromo23 wrote: 09 Mar 2023, 17:38 Edit: I personally find it hard to understand what you want to achieve. Maybe you can explain it for me a bit simpler.

User avatar
chromo23
Normal user
Posts: 821
Joined: 10 Sep 2020, 16:02
Location: germany

Re: TaskValueSet

#9 Post by chromo23 » 10 Mar 2023, 09:28

abhelou wrote: 08 Mar 2023, 23:42 yes it is enabled. on devices page, i can see state turn to 0, and light turn off, but when i click the switch again, it seems like it is still 1, but is shows 0. so i have to click once again to be able to toggle state to 1
I think i know what your issue is.

You have a toggle switch, which sets a GPIO either high or low.
Lets assume, you bring your switch to the "on" position, which means the GPIO is "high" and "state" becomes 1 (depending on your settings):
GPIO = high
State = 1


now you use taskvalueset to change the state to 0. that doesn´t mean you set the hardware state:
GPIO = high
State = 0


If you would now switch the toggle switch to the "off" position:
GPIO = low
State = 0


So either you use a push button or you have to rethink your whole concept...

Edit: actually the GPIO becomes "low" when you have connected it to ground and use a pullup. But that has no relevance for making the point.

abhelou
Normal user
Posts: 19
Joined: 22 Apr 2018, 21:29

Re: TaskValueSet

#10 Post by abhelou » 12 Mar 2023, 13:10

chromo23 wrote: 10 Mar 2023, 09:28
abhelou wrote: 08 Mar 2023, 23:42 yes it is enabled. on devices page, i can see state turn to 0, and light turn off, but when i click the switch again, it seems like it is still 1, but is shows 0. so i have to click once again to be able to toggle state to 1
I think i know what your issue is.

You have a toggle switch, which sets a GPIO either high or low.
Lets assume, you bring your switch to the "on" position, which means the GPIO is "high" and "state" becomes 1 (depending on your settings):
GPIO = high
State = 1


now you use taskvalueset to change the state to 0. that doesn´t mean you set the hardware state:
GPIO = high
State = 0


If you would now switch the toggle switch to the "off" position:
GPIO = low
State = 0


So either you use a push button or you have to rethink your whole concept...

Edit: actually the GPIO becomes "low" when you have connected it to ground and use a pullup. But that has no relevance for making the point.
exactly!
i wonder if there is any function in rules to bring gpio low because in my rule,if i add "GPIO 1,0", it won't turn on anymore

on sw#state do
if %eventvalue%=0 and [dummy#pwm]<1023
TaskValueSet 3,1,1023
elseif %eventvalue%=1 and [dummy#pwm]=1023
TaskValueSet 3,1,0
elseif %eventvalue%=1 and [dummy#pwm]<1023
TaskValueSet 3,1,1023
GPIO 1,0
TaskValueSet 1,1,0
endif
endon

on rotary#counter do
TaskValueSet 3,1,1023-(%eventvalue%*10.23)
endon

on dummy#pwm do
PWM,2,%eventvalue%
endon
Attachments
2.png
2.png (26.84 KiB) Viewed 1695 times

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: TaskValueSet

#11 Post by Ath » 12 Mar 2023, 13:49

You haven't disclosed what type of switch is connected to GPIO-1, a push-button or an on/off switch?

I'd go for keeping the state not in the switch, but in the Dummy Device, by changing that to Dual, and naming the second value State. Then, when the switch is pressed, flip the Dummy#State value between 0 and 1, and the same goes for when a different source decides that the State should be on or off.
NB: The Interval for the Dummy device must be set to 0! We don't want any extra events generated (that's why I've used TaskValueSetAndRun in most commands, and I've used the taskname,valuename notation for readability)

Code: Select all

On sw#State Do // Use Switch#State=1 when having a push-button connected
  TaskValueSetAndRun,Dummy,State,![Dummy#State] // Flip state
Endon

On Dummy#State Do
  if %eventvalue%=0 and [dummy#pwm]<1023
    TaskValueSetAndRun Dummy,Pwm,1023
  elseif %eventvalue%=1 and [dummy#pwm]=1023
    TaskValueSetAndRun Dummy,Pwm,0
  elseif %eventvalue%=1 and [dummy#pwm]<1023
    TaskValueSetAndRun Dummy,Pwm,1023
    TaskValueSet,Dummy,State,0 // Turn off, no event
  endif
Endon

On Rotary#Counter Do
  TaskValueSetAndRun Dummy,Pwm,1023-(%eventvalue%*10.23)
Endon

On Dummy#Pwm Do
  PWM,2,%eventvalue1%
Endon
NB: For readability, please use Code tags when sharing code here (</> button in the forum-editor toolbar).
/Ton (PayPal.me)

abhelou
Normal user
Posts: 19
Joined: 22 Apr 2018, 21:29

Re: TaskValueSet

#12 Post by abhelou » 12 Mar 2023, 16:03

by the way, my switch configuration is push button active high. anything i can do to switch state in rules while using this type of switch?

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: TaskValueSet

#13 Post by Ath » 12 Mar 2023, 16:23

abhelou wrote: 12 Mar 2023, 16:03 by the way, my switch configuration is push button active high. anything i can do to switch state in rules while using this type of switch?
That plugin is intended to reflect the state of a switch connected to a GPIO pin, not to emulate an electronic button, so there are no commands to manipulate the state, other than TaskValueSet with the Tools/Advanced settings option to allow that to be changed. But the plugin doesn't really take that state into account when pressing the button.

That switch configuration is somewhat non-standard. When using push-buttons, it is usual to have a pull-up resistor on the GPIO pin, and use the button to connect the GPIO to ground. Using the Push-button configuration option may cause the behavior you have reported, having to press it twice to change the state to what you expect.
When using my latest suggestion for rules, you could change it to Normal Switch, and change in Rules "on sw#State do" to "on sw#State=1 do", so it will respond to you releasing the button (a common configuration). You can also replace the 1 by 0, then it will respond at the moment you press the button (and connect the GPIO to GND, as I wrote above).
/Ton (PayPal.me)

abhelou
Normal user
Posts: 19
Joined: 22 Apr 2018, 21:29

Re: TaskValueSet

#14 Post by abhelou » 12 Mar 2023, 19:01

Ath wrote: 12 Mar 2023, 16:23
abhelou wrote: 12 Mar 2023, 16:03 by the way, my switch configuration is push button active high. anything i can do to switch state in rules while using this type of switch?
That plugin is intended to reflect the state of a switch connected to a GPIO pin, not to emulate an electronic button, so there are no commands to manipulate the state, other than TaskValueSet with the Tools/Advanced settings option to allow that to be changed. But the plugin doesn't really take that state into account when pressing the button.

That switch configuration is somewhat non-standard. When using push-buttons, it is usual to have a pull-up resistor on the GPIO pin, and use the button to connect the GPIO to ground. Using the Push-button configuration option may cause the behavior you have reported, having to press it twice to change the state to what you expect.
When using my latest suggestion for rules, you could change it to Normal Switch, and change in Rules "on sw#State do" to "on sw#State=1 do", so it will respond to you releasing the button (a common configuration). You can also replace the 1 by 0, then it will respond at the moment you press the button (and connect the GPIO to GND, as I wrote above).
something is forcing dummy state to zero. when using encoder, pwm changes then it goes back to 0. if clicking the switch, it also return back immediately to zero.
i modified it to

Code: Select all

On sw#State=1 Do // Use Switch#State=1 when having a push-button connected
  TaskValueSetAndRun,Dummy,State,![Dummy#State] // Flip state
Endon

On Dummy#State Do
  if %eventvalue%=0 and [dummy#pwm]<1023
    TaskValueSet Dummy,Pwm,1023
  elseif %eventvalue%=1 and [dummy#pwm]=1023
    TaskValueSet Dummy,Pwm,0
  elseif %eventvalue%=1 and [dummy#pwm]<1023
    TaskValueSet Dummy,Pwm,1023
    TaskValueSet,Dummy,State,0 // Turn off, no event
  endif
Endon

On Rotary#Counter Do
  TaskValueSetAndRun Dummy,Pwm,1023-(%eventvalue%*10.23)
Endon

On Dummy#Pwm Do
  PWM,2,%eventvalue1%
Endon
now it is working as it should, but dummy pwm is always showing 1023, but the lamp is working like it should. if i modify on rotary to taskvalueset in spite of taskbaluesetandrun, dummy pwm will show the right value, but the pwm output wouldn't change

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: TaskValueSet

#15 Post by Ath » 12 Mar 2023, 19:46

Do you have an Interval > 0 set on the Dummy Device?
/Ton (PayPal.me)

abhelou
Normal user
Posts: 19
Joined: 22 Apr 2018, 21:29

Re: TaskValueSet

#16 Post by abhelou » 12 Mar 2023, 19:59

Ath wrote: 12 Mar 2023, 19:46 Do you have an Interval > 0 set on the Dummy Device?
no, all intervals for dummy and encoder are 0

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: TaskValueSet

#17 Post by Ath » 12 Mar 2023, 20:53

What version of ESPEasy are you running? (Name of the .bin file installed, please, should be shown on every page bottom right, and if not, on the Tools/Info page)
abhelou wrote: 12 Mar 2023, 19:59 no, all intervals for dummy and encoder are 0
If the Interval is set to 0, then no event is generated when the value for Dummy,Pwm is set via TaskValueSet. Is there another way that the PWM command is triggered after that value is updated?

The value is not updated on the devices page as there (probably) is no Interval set for all your tasks, and the lowest Interval setting is used to refresh that page. 0 means no refresh, but reloading the page, or clicking on the Devices tab, will update the values displayed. Where using TaskValueSetAndRun will actually refresh that page as it also 'runs' the task, and also generate the event, updating the PWM value.
/Ton (PayPal.me)

abhelou
Normal user
Posts: 19
Joined: 22 Apr 2018, 21:29

Re: TaskValueSet

#18 Post by abhelou » 14 Mar 2023, 12:33

Ath wrote: 12 Mar 2023, 20:53 What version of ESPEasy are you running? (Name of the .bin file installed, please, should be shown on every page bottom right, and if not, on the Tools/Info page)
abhelou wrote: 12 Mar 2023, 19:59 no, all intervals for dummy and encoder are 0
If the Interval is set to 0, then no event is generated when the value for Dummy,Pwm is set via TaskValueSet. Is there another way that the PWM command is triggered after that value is updated?

The value is not updated on the devices page as there (probably) is no Interval set for all your tasks, and the lowest Interval setting is used to refresh that page. 0 means no refresh, but reloading the page, or clicking on the Devices tab, will update the values displayed. Where using TaskValueSetAndRun will actually refresh that page as it also 'runs' the task, and also generate the event, updating the PWM value.
after many trials and log monitoring, i noticed that when taskvalutsetandrun, it is weird that it is running both output even when only one of them is triggered. you code has worked smoothly, all i had to do is created every dummy under different task.i'm not sure if it's a bug or it is intended to run all output when one output is triggered

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: TaskValueSet

#19 Post by TD-er » 14 Mar 2023, 12:52

TaskValueSetAndRun is just a shortcut for TaskValueSet and TaskRun.

So a typical usecase is this:

Code: Select all

  TaskValueSet,gps1,long,[gps#long] 
  TaskValueSet,gps1,lat,[gps#lat] 
  TaskValueSet,gps1,alt,[gps#alt] 
  TaskValueSet,gps1,spd,[gps#spd] 
  TaskRun,gps1
The last 2 commands can be replaced by a single call to TaskValueSetAndRun,gps1,spd,[gps#spd]

And yes, it is intended to 'flush' all taskvalues to the linked controller(s) when a "TaskRun" is called.

Typically, for plugins which yield a sensor value (or several values), you have some interval on which the task is calling PLUGIN_READ
When a call to this (internal) function yields a success, then all taskvalues are sent to the linked controller(s).

What the TaskRun command does, is that it calls the internal PLUGIN_READ.

On dummy tasks, this will always return a success = true and it makes less sense to have a dummy task running at a fixed interval.
Therefore the TaskRun command was added. But it does make sense to also use this on other tasks, to trigger a new reading.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 25 guests