how to change switch-icon on the fly

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
BartSr
Posts: 347
Joined: Sunday 03 July 2016 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.3
Location: Netherlands
Contact:

how to change switch-icon on the fly

Post by BartSr »

I am controlling a light in the garden via an url sent from Domoticz to arduino. e.g.:
http://192.168.2.17/LED=ON and http://192.168.2.17/LED=OFF
Up to now the switch icon immediately turns into yellow-color but I only want to change the switch icon if it's sure the light is on. (Same as with Zwave switches.)

Communication in two ways works already fine for me but it's unclear how to change an icon control.
e.g. set a var named 'LightIsOn' [1=on; 0 I=off]. depending on status of that var the icon should change.

So if the light is on Arduino (in the garden) sents:Domoticz_IP:8080//json.htm?type=command&param=updateuservariable&vname=USERVARIABLENAME&vtype=USERVARIABLETYPE
&vvalue=USERVARIABLEVALUE. A contact of a relay which is connected across the lamp informs Arduino : light is on.

Basically same procedure will be applied later on in the project to show whether or not a gardendoor has been locked (by activation of inductive limit switch). In that case only comm from Arduino towards Domoticz.


Any suggestion?

TIA
-Bart
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 espeasy
MQTT
BartSr
Posts: 347
Joined: Sunday 03 July 2016 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.3
Location: Netherlands
Contact:

Re: how to change switch-icon on the fly

Post by BartSr »

Hi.
Over 2 years since I asked about changing icons on the fly but never got a suggestion.
Maybe now?
TIA
-Bart
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 espeasy
MQTT
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: how to change switch-icon on the fly

Post by waaren »

BartSr wrote: Sunday 03 January 2021 22:33 Over 2 years since I asked about changing icons on the fly but never got a suggestion.
Maybe now?

Code: Select all

http://<domoticz ip: domoticz port>/json.htm?type=setused&used=true&name=<name>&description=<description>&idx=<idx>&switchtype=<switchType>&customimage=<iconNumber>
or in a dzVents script:

Code: Select all

domoticz.devices('device name').setIcon('icon number')
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
jeroenkl
Posts: 113
Joined: Sunday 14 July 2013 22:00
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: NL
Contact:

Re: how to change switch-icon on the fly

Post by jeroenkl »

waaren wrote: Monday 04 January 2021 0:26
BartSr wrote: Sunday 03 January 2021 22:33 Over 2 years since I asked about changing icons on the fly but never got a suggestion.
Maybe now?

Code: Select all

http://<domoticz ip: domoticz port>/json.htm?type=setused&used=true&name=<name>&description=<description>&idx=<idx>&switchtype=<switchType>&customimage=<iconNumber>
or in a dzVents script:

Code: Select all

domoticz.devices('device name').setIcon('icon number')
Hi Waaren,
Is it possible with this dzVents command to change the Icon 'text' at a custom dummy created device?

BR, Jeroen
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: how to change switch-icon on the fly

Post by waaren »

jeroenkl wrote: Monday 04 January 2021 16:41 Is it possible with this dzVents command to change the Icon 'text' at a custom dummy created device?
Yes but you need to be on the Beta version build >= 12750.
Please note tat the iconchange will only be visible when you reopen the screen or after you manually refresh [(ctrl] r in Chrome) or switch back and forth via another screen.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
jeroenkl
Posts: 113
Joined: Sunday 14 July 2013 22:00
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: NL
Contact:

Re: how to change switch-icon on the fly

Post by jeroenkl »

waaren wrote: Monday 04 January 2021 17:24
jeroenkl wrote: Monday 04 January 2021 16:41 Is it possible with this dzVents command to change the Icon 'text' at a custom dummy created device?
Yes but you need to be on the Beta version build >= 12750.
Please note tat the iconchange will only be visible when you reopen the screen or after you manually refresh [(ctrl] r in Chrome) or switch back and forth via another screen.
Great!

Ehh @waaren, how do I find the Icon number? I've got the name but not a number

BR,
Jeroen
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: how to change switch-icon on the fly

Post by waaren »

jeroenkl wrote: Tuesday 05 January 2021 15:59 how do I find the Icon number? I've got the name but not a number
Maybe there are better / easier ways but I do

Code: Select all

cd <domoticz dir>
sudo sqlite3 -header domoticz.db " select id+100 id ,name from customimages"
to get the to be used number.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
BartSr
Posts: 347
Joined: Sunday 03 July 2016 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.3
Location: Netherlands
Contact:

Re: how to change switch-icon on the fly

Post by BartSr »

Hi Waaren,

Thanks for this info.
I played a bit but for me it seems to academic (syntax).
Would it be possible that you give kind of practic example?

TIA
-Bart
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 espeasy
MQTT
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: how to change switch-icon on the fly

Post by waaren »

BartSr wrote: Friday 15 January 2021 19:46 I played a bit but for me it seems to academic (syntax).
Would it be possible that you give kind of practic example?
Sure, if you have a dzVents script in where you want to use the setIcon() method and an icon number you uploaded, I will show you how it can be done.

I use it in below dzVents script but because of some of the other techniques used in that code I don't know if it will help or confuse you more.

Code: Select all

--[[ 
        dz Moonphase script
        Version 1.03
        dzVents >= 3.0.1)
        More info https://github.com/JoopIdema/dz-MoonPhases and dz forum https://www.domoticz.com/forum/search.php?keywords=Moonphase
]]--

local scriptVar = 'moonScript'
local api_key = 'Obscured'
local town = 'Rotterdam'

return 
{
    on = 
    {
        timer = 
        { 
            'at 00:11',
        },

        httpResponses = 
        { 
            scriptVar,
        },

    },

    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)

        if not( item.isHTTPResponse ) then
            dz.openURL(
            {
                url = 'https://weather.ls.hereapi.com/weather/1.0/report.json?apiKey=' .. api_key .. '&product=forecast_astronomy&name=' .. town ,
                method = 'GET',
                callback = scriptVar,
            })
            return
        end

        local firstIcon = 100
        local moonPhaseIDX = 164
        
        local function make24hTime(myTime) 
            local pattern = "(%d+)%:(%d+)(%w*)"
            local hour, minute, meridiem = myTime:match(pattern)
            return string.format("%02d:%02d", ( meridiem == 'PM' and ( hour or 0 ) + 12 or ( hour or 0 )), minute or 0) 
        end

        local function makeMoonPhase(phase) 
            local moonPhaseTable = 
                {
                    ['new moon'] = { 1, 'Nieuwe maan'},
                    ['waxing crescent'] = { 2, 'Wassende halve maan'},
                    ['first quarter'] = { 3, 'Eerste kwartier'},
                    ['waxing gibbous'] = { 4, 'Wassende maan'},
                    ['full moon'] = { 5, 'Volle maan'},
                    ['waning gibbous'] = { 6, 'Afnemende maan'},
                    ['last quarter'] = { 7, 'Laatste kwartier'},
                    ['waning crescent'] = { 8, 'Afnemende halve maan'},
                }
            return moonPhaseTable[phase:lower()][1] + firstIcon, moonPhaseTable[phase:lower()][2] 
        end

        if item.ok and item.isJSON then
            local moonPhase = dz.devices('MoonPhase')
            
            -- dz.utils.dumpTable(item.json)
            
            local rt = item.json.astronomy.astronomy[1]
            
            local moonPhasePercentage = math.abs(rt.moonPhase * 100)
            local moonPhaseIconNumber, moonPhaseName= makeMoonPhase(rt.moonPhaseDesc)
            local currentIcon =  math.tointeger((moonPhase.icon):gsub('MoonPhases',''):sub(1,1) + firstIcon)
            
            
            dz.devices('MoonPhaseDesc').updateText(moonPhaseName .. '\nmoonrise: ' .. make24hTime(rt.moonrise) ..  '\nmoonset:  ' .. make24hTime(rt.moonset))
            dz.devices('MoonPhase').updateCustomSensor(moonPhasePercentage)
            
            if currentIcon ~= moonPhaseIconNumber then dz.devices('MoonPhase').setIcon(moonPhaseIconNumber) end
            

            dz.log('moonPhasePercentage: ' .. moonPhasePercentage, dz.LOG_DEBUG)
           dz.log('moonPhaseIcon: ' .. dz.devices('MoonPhase').icon, dz.LOG_DEBUG)
         else
            dz.log('There was a problem handling the request', dz.LOG_ERROR)
            dz.log(item, dz.LOG_ERROR)
        end

    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
BartSr
Posts: 347
Joined: Sunday 03 July 2016 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.3
Location: Netherlands
Contact:

Re: how to change switch-icon on the fly

Post by BartSr »

Dear Waaren, I hoped you might make available an example using the http:/domoticz-ip,domoticz-port etc syntax.
That way It would be possible to change an icon from a device such as esp-easy as well other network devices too. Can you give me a hand on this?
Thanks
Bart
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 espeasy
MQTT
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: how to change switch-icon on the fly

Post by waltervl »

Why not make a virtual switch instead of a text device? Then you have an on/off icon. If you want to make a custom icon garden lights then you can do that too. No need to do a complicated thing with scripts, just update the device status on/off with the domotica API/json from Arduino.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: how to change switch-icon on the fly

Post by waaren »

BartSr wrote: Saturday 16 January 2021 16:22 Dear Waaren, I hoped you might make available an example using the http:/domoticz-ip,domoticz-port etc syntax.
That way It would be possible to change an icon from a device such as esp-easy as well other network devices too. Can you give me a hand on this?
I already did earlier in this topic.
in this post
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
BartSr
Posts: 347
Joined: Sunday 03 July 2016 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.3
Location: Netherlands
Contact:

Re: how to change switch-icon on the fly

Post by BartSr »

Dear Waaren. That one I referred to when I spoke about "academic" .
I know once you know how to interprete the syntax it's pretty easy. But the otherway around it might, as for me, have an academic look....
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 espeasy
MQTT
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: how to change switch-icon on the fly

Post by waaren »

BartSr wrote: Sunday 17 January 2021 12:32 Dear Waaren. That one I referred to when I spoke about "academic" .
I know once you know how to interprete the syntax it's pretty easy. But the otherway around it might, as for me, have an academic look....

Code: Select all

http://<domoticz ip: domoticz port>/json.htm?type=setused&used=true&name=<name>&description=<description>&idx=<idx>&switchtype=<switchType>&customimage=<iconNumber>
for me this translates to

Code: Select all

http://192.168.192.115:8084/json.htm?type=setused&used=true&name=MoonPhase&description=&idx=164&switchtype=0&customimage=102
where MoonPhase is the name of my Sensor (which does not have anything in the description field)
the idx of this Sensor = 164 and is a Generic device type (0)
The customImage number can be found
using

Code: Select all

cd <domoticz dir>
sudo sqlite3 -header domoticz.db " select id+100 id ,name from customimages"
for me this translates to

Code: Select all

cd /opt/domoticz
sudo sqlite3 -header domoticz.db " select id+100 id ,name from customimages"
Spoiler: show

Code: Select all

192.168.192.115:/opt/domoticz: sudo sqlite3 -header domoticz.db " select id+100 id ,name from customimages"
id|Name
101|MoonPhases1NM
102|MoonPhases2WC
103|MoonPhases3FQ
104|MoonPhases4WG
105|MoonPhases5FM
106|MoonPhases6WG
107|MoonPhases7LQ
108|MoonPhases8WC
109|Chromecast
110|GoogleHome
111|Alarm_tone
112|arm
113|cctv3
114|door_locked
115|door_sensor
116|eye
117|flame_sensor
118|Heat_sensor
119|home_locked
120|keypad
121|keypad2
122|keyset
123|locked
124|motion_detection
125|motion_sensor2
126|secure_locled
127|smoke_detector
128|unlocked
129|Voip_phone
130|windows_sensor
131|Window
132|Z-wave node
I hope this clarifies it for you
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
BartSr
Posts: 347
Joined: Sunday 03 July 2016 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.3
Location: Netherlands
Contact:

Re: how to change switch-icon on the fly

Post by BartSr »

Waaren, thanks a lot. This makes sense to me. Going to try and will of course revert with results.
-Bart
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 espeasy
MQTT
Kiker
Posts: 1
Joined: Friday 23 February 2024 14:14
Target OS: Linux
Domoticz version:
Contact:

Re: how to change switch-icon on the fly

Post by Kiker »

Code: Select all

http://<domoticz ip: domoticz port>/json.htm?type=setused&used=true&name=<name>&description=<description>&idx=<idx>&switchtype=<switchType>&customimage=<iconNumber>
..after playing with changing the icons in domotic it helped me a lot..
but I can't figure out how to actually change the icons to on or off. If I write down the numbers assigned to the icons, the group of icons (where there are two icons on and off) has only one number...
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests