My 'Counter' is a virtual sensor, type Text. That is why I retrieve the info with otherdevices_svalues and convert it to a number so that I can calculate with it. When I read your 1st question, it wasn't clear to me what you wanted. Stating that an 'Always On' state is a problem, was something I doubted. I proved that it could work, with my video, I think.
Just typing over the code doesn't work. For instance, the '2' in 'commandArray['UpdateDevice'] = 2 .. '|0|' .. tostring(counter)' refers to the idx of my virtual Text sensor. The idx of your 'Keukenlevel' is probably not 2. And the '()' in 'counter = otherdevices_svalues('KeukenLevel')' needs to be '[]'.
What I did was:
create a switch, type Push On Button, name: PushOnButton
create a virtual sensor, type Text, name: Counter
Find the Idx for the virtual sensor (in Setup - Devices), in my case it is 2.
Then the script:
Code: Select all
counter = otherdevices_svalues['Counter']
counter = tonumber(counter)
commandArray = {}
if
devicechanged['PushOnButton'] == 'On'
then
counter = counter + 1
commandArray['UpdateDevice'] = 2 .. '|0|' .. tostring(counter)
end
return commandArray
This script won't work the first time, because the text sensor has no value. So you have to fill this text sensor with a numeric value. You can do that by replacing the first line with 'counter = 0'. Then revert the change.
After that, this script will read the value (as a string), then turns it into a number, and when the button is pressed, it will add 1 to the value and write is as a string in the text sensor.
Regards,
Martin