Page 1 of 1
Can a selector levelname be changed with script?
Posted: Friday 16 August 2024 17:47
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?
Re: Can a selector levelname be changed with script?
Posted: Friday 16 August 2024 17:57
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.
Re: Can a selector levelname be changed with script?
Posted: Friday 16 August 2024 18:29
by HvdW
Is
this helpfull?
Found it in
the DzVents wiki.
Re: Can a selector levelname be changed with script?
Posted: Friday 16 August 2024 18:49
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=¶m=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
Re: Can a selector levelname be changed with script?
Posted: Friday 16 August 2024 19:15
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¶m=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.
Re: Can a selector levelname be changed with script?
Posted: Friday 16 August 2024 19:18
by janpep
I have seen that, but there is no option to change Selector LevelName of a Selector Level.
Re: Can a selector levelname be changed with script?
Posted: Friday 16 August 2024 19:22
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.
Re: Can a selector levelname be changed with script?
Posted: Friday 16 August 2024 19:24
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.
Re: Can a selector levelname be changed with script?
Posted: Friday 16 August 2024 19:40
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.
Re: Can a selector levelname be changed with script?
Posted: Friday 16 August 2024 22:40
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 .. '¶m=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 (39.29 KiB) Viewed 1338 times