Can a selector levelname be changed with script?

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
janpep
Posts: 270
Joined: Thursday 14 March 2024 10:11
Target OS: Linux
Domoticz version: 2025.1
Location: Netherlands
Contact:

Can a selector levelname be changed with script?

Post by janpep »

I am experimenting with a selector switch and I would like to be able to dynamically set the level name (in my case only for level 0).
Then setting to level = 0 when it is OFF with a certain levelname, while changing to another level (with the same levelname) sets it ON.
That state (On or Off) is visible for the human eye when you look at the icon and it is visible by the script where level == 0 or level > 0.
I did not find a way to change levelname of a selector switch with DzVents.
Anyone with an idea?
Dz on Ubuntu VM on DS718+ behind FRITZ!Box.
EvoHome; MELCloud; P1 meter; Z-Stick GEN5; Z-Wave-js-ui; Sonoff USB-Dongle Plus-E; Zigbee2Mqtt; MQTT; Greenwave powernodes 1+6; Fibaro switch, plugs, smoke; FRITZ!DECT 200. Scripts listed in profile interests.
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Can a selector levelname be changed with script?

Post by jvdz »

I am on my phone so can't check, but I would open chrome in dev mode(F12), goto the Domoticz webfrontend and edit the switch manually. Then look at the network tab and check the json API call made to perform the change.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
HvdW
Posts: 617
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Can a selector levelname be changed with script?

Post by HvdW »

Is this helpfull?
Found it in the DzVents wiki.
Bugs bug me.
FlyingDomotic
Posts: 388
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Can a selector levelname be changed with script?

Post by FlyingDomotic »

API called to change selector settings (an probably all other types, when loading proper values) is :

Code: Select all

http://<domoticz IP>/json.htm?addjvalue=0&addjvalue2=0&customimage=0&description=&idx=2254&name=Selector&options=TGV2ZWxOYW1lczpPZmZ8TGV2ZWwgMXxMZXZlbCAyfExldmVsIDM7TGV2ZWxBY3Rpb25zOnx8fDtTZWxlY3RvclN0eWxlOjA7TGV2ZWxPZmZIaWRkZW46ZmFsc2U=&param=setused&protected=false&strparam1=&strparam2=&switchtype=18&type=command&used=true
In my case, selector name was "Selector", level 0 was "Off", level 10 was "Level 1", level 20 was "Level 2", level 30 was "Level 3".

Levels are hidden into "options=", which are base 64 encoded.

Decoding

Code: Select all

TGV2ZWxOYW1lczpPZmZ8TGV2ZWwgMXxMZXZlbCAyfExldmVsIDM7TGV2ZWxBY3Rpb25zOnx8fDtTZWxlY3RvclN0eWxlOjA7TGV2ZWxPZmZIaWRkZW46ZmFsc2U
gives

Code: Select all

LevelNames:Off|Level 1|Level 2|Level 3;LevelActions:|||;SelectorStyle:0;LevelOffHidden:false
Process to make the change is probably intercepting a manual change of this exact device, to get the right parameters, then decoding options, to get the right ones, manually change level(s) names, re-encode them in base 64, and call the API.

You may find here some help if this seems to be complicated
janpep
Posts: 270
Joined: Thursday 14 March 2024 10:11
Target OS: Linux
Domoticz version: 2025.1
Location: Netherlands
Contact:

Re: Can a selector levelname be changed with script?

Post by janpep »

jvdz wrote: Friday 16 August 2024 17:57 I am on my phone so can't check, but I would open chrome in dev mode(F12) <cut>
Great idea. But the result is more complicated then I thought. :-)
It apears that the options are send as an encoded string. See the options part.

Code: Select all

https://MyDomain:MyPort/json.htm?addjvalue=0&addjvalue2=0&customimage=144&description=&idx=1037&name=Vakantiebestemming&options=TGV2ZWxOYW1lczpOTCxCRXxOTCxCRSxMVSxERXxOTCxCRSxMVSxGUnxOTCxCRSxMVSxGUixFU3xOTCxFU3xOTCxERSxDSHxOTCxERSxDSCxJVHxOTCxERSxDSCxBVHxOTCxERSxDSCxBVCxJVDtMZXZlbEFjdGlvbnM6fHx8fHx8fHw7U2VsZWN0b3JTdHlsZToxO0xldmVsT2ZmSGlkZGVuOmZhbHNl&param=setused&protected=false&strparam1=&strparam2=&switchtype=18&type=command&used=true
When I try a base64 decoder, I get the content of my experiment.

Code: Select all

LevelNames:NL,BE|NL,BE,LU,DE|NL,BE,LU,FR|NL,BE,LU,FR,ES|NL,ES|NL,DE,CH|NL,DE,CH,IT|NL,DE,CH,AT|NL,DE,CH,AT,IT;LevelActions:||||||||;SelectorStyle:1;LevelOffHidden:false
It is not my preferred way, but I will also take a look how this is stored in the database, because to encode a new content in this way might lead to problems when manual changes (e.g. added options) have taken place and the script is not adapted to that. Then an sql command may be less risky.
Dz on Ubuntu VM on DS718+ behind FRITZ!Box.
EvoHome; MELCloud; P1 meter; Z-Stick GEN5; Z-Wave-js-ui; Sonoff USB-Dongle Plus-E; Zigbee2Mqtt; MQTT; Greenwave powernodes 1+6; Fibaro switch, plugs, smoke; FRITZ!DECT 200. Scripts listed in profile interests.
janpep
Posts: 270
Joined: Thursday 14 March 2024 10:11
Target OS: Linux
Domoticz version: 2025.1
Location: Netherlands
Contact:

Re: Can a selector levelname be changed with script?

Post by janpep »

HvdW wrote: Friday 16 August 2024 18:29 Is this helpfull?
Found it in the DzVents wiki.
I have seen that, but there is no option to change Selector LevelName of a Selector Level.
Dz on Ubuntu VM on DS718+ behind FRITZ!Box.
EvoHome; MELCloud; P1 meter; Z-Stick GEN5; Z-Wave-js-ui; Sonoff USB-Dongle Plus-E; Zigbee2Mqtt; MQTT; Greenwave powernodes 1+6; Fibaro switch, plugs, smoke; FRITZ!DECT 200. Scripts listed in profile interests.
janpep
Posts: 270
Joined: Thursday 14 March 2024 10:11
Target OS: Linux
Domoticz version: 2025.1
Location: Netherlands
Contact:

Re: Can a selector levelname be changed with script?

Post by janpep »

FlyingDomotic wrote: Friday 16 August 2024 18:49 Levels are hidden into "options=", which are base 64 encoded.
Yes, Thank you.
I came to the same conclusion, but a bit risky to edit the option this way.
I will play with it in my test environment, but first I want to look into the database where and how it is stored. That may be easier.
Dz on Ubuntu VM on DS718+ behind FRITZ!Box.
EvoHome; MELCloud; P1 meter; Z-Stick GEN5; Z-Wave-js-ui; Sonoff USB-Dongle Plus-E; Zigbee2Mqtt; MQTT; Greenwave powernodes 1+6; Fibaro switch, plugs, smoke; FRITZ!DECT 200. Scripts listed in profile interests.
FlyingDomotic
Posts: 388
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Can a selector levelname be changed with script?

Post by FlyingDomotic »

janpep wrote: Friday 16 August 2024 19:15 It is not my preferred way, but I will also take a look how this is stored in the database, because to encode a new content in this way might lead to problems when manual changes (e.g. added options) have taken place and the script is not adapted to that. Then an sql command may be less risky.
Amazingly, data is base64 encoded into database (but good news, options names are in clear text) ;-)

FYI, table is "DeviceStatus", column is "Options". For my previous example, content is:

Code: Select all

LevelActions:fHx8;LevelNames:T2ZmfExldmVsIDF8TGV2ZWwgMnxMZXZlbCAz;LevelOffHidden:ZmFsc2U=;SelectorStyle:MA==
[Edit]: and personally, I would prefer using API which will do the job properly, not forgetting any link between relations, instead of writing into the database. And when I do write into database, I stop Domoticz before, take a database copy, make changes and restart Domoticz, after having checked my changes carefully.
janpep
Posts: 270
Joined: Thursday 14 March 2024 10:11
Target OS: Linux
Domoticz version: 2025.1
Location: Netherlands
Contact:

Re: Can a selector levelname be changed with script?

Post by janpep »

FlyingDomotic wrote: Friday 16 August 2024 19:24 FYI, table is "DeviceStatus", column is "Options". For my previous example, content is:
You are fast :)
I was still looking in the wiki and found that 'toBase64(string)' is indeed available in the utils.
I will look into that first and give it a try in my test environment.
Thanks again.
Dz on Ubuntu VM on DS718+ behind FRITZ!Box.
EvoHome; MELCloud; P1 meter; Z-Stick GEN5; Z-Wave-js-ui; Sonoff USB-Dongle Plus-E; Zigbee2Mqtt; MQTT; Greenwave powernodes 1+6; Fibaro switch, plugs, smoke; FRITZ!DECT 200. Scripts listed in profile interests.
janpep
Posts: 270
Joined: Thursday 14 March 2024 10:11
Target OS: Linux
Domoticz version: 2025.1
Location: Netherlands
Contact:

Re: Can a selector levelname be changed with script?

Post by janpep »

So far so good. It works, but I have to refresh to see the new value.

Code: Select all

if dz.devices(vacation_idx).state == 'Off' then
    -- Destination changed, but VacationMode not started but set.
    if dz.devices(destination_idx).levelVal > 0 then
        --Set the defaultSelectorOpotions in a string.
        local defaultSelectorOptions = 'LevelNames:LEVEL0|NL|NL,BE,LU,DE|NL,BE,LU,FR|NL,BE,LU,FR,ES|NL,ES|NL,DE,CH|NL,DE,CH,IT|NL,DE,CH,AT|NL,DE,CH,AT,IT;LevelActions:|||||||||;SelectorStyle:1;LevelOffHidden:false'
        -- Get destination from the selected item.
        local destination = dz.devices(destination_idx).levelName --get destination from selection.
        --Replace LEVEL0 in the string by this selection.
        local stringToEncode = string.gsub( defaultSelectorOptions, "LEVEL0", destination )
        --Encode the new string.
        local encodedString = _u.toBase64( stringToEncode )
        --Change the deviceoptions.
        dz.executeShellCommand('curl "http://127.0.0.1:portnumber/json.htm?addjvalue=0&addjvalue2=0&customimage=144&description=&idx=' ..destination_idx .. '&name=Vakantiebestemming&options=' .. encodedString ..  '&param=setused&protected=false&strparam1=&strparam2=&switchtype=18&type=command&used=true" ')
        -- Select level 0, to that the switch icon is off. It will be turned on when VacationMode starts.
        dz.devices(destination_idx).switchSelector(0)       --goto level 0
    end
end
VacationMode-4-JanPep.png
VacationMode-4-JanPep.png (39.29 KiB) Viewed 1326 times
Dz on Ubuntu VM on DS718+ behind FRITZ!Box.
EvoHome; MELCloud; P1 meter; Z-Stick GEN5; Z-Wave-js-ui; Sonoff USB-Dongle Plus-E; Zigbee2Mqtt; MQTT; Greenwave powernodes 1+6; Fibaro switch, plugs, smoke; FRITZ!DECT 200. Scripts listed in profile interests.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest