Cannot Use Domoticz.image from plugin to update sensor image

Python and python framework

Moderator: leecollings

Post Reply
User avatar
ycahome
Posts: 248
Joined: Sunday 12 February 2017 10:55
Target OS: Linux
Domoticz version: lat Beta
Contact:

Cannot Use Domoticz.image from plugin to update sensor image

Post by ycahome »

Hello,

i have created one thread on "Bugs and Threads" but i don't realy know if its a problems or its "by design".

http://www.domoticz.com/forum/viewtopic.php?f=6&t=19534


So, my question is: Can i use "Domoticz.Image(icon)" and "Devices[1].Update(0, str(1), Image=Images[icon].ID)" to continuously change the icon of a virtual sensor (for example an Alert Sensor)?

What am trying to achieve here is to create a plugin that will have the moon phases and icon will show the current phase.

Any help will be appreciated,
Thanks
Last edited by ycahome on Monday 02 October 2017 12:25, edited 1 time in total.
zak45
Posts: 953
Joined: Sunday 22 January 2017 11:37
Target OS: Windows
Domoticz version: V2024.4
Contact:

Re: Use Domoticz.image from plugin to update sensor image

Post by zak45 »

Should be possible, but I think that your sensor device need to be created by the plugin itself.
see http://www.domoticz.com/wiki/Plugins/BatteryLevel.html
User avatar
ycahome
Posts: 248
Joined: Sunday 12 February 2017 10:55
Target OS: Linux
Domoticz version: lat Beta
Contact:

Re: Use Domoticz.image from plugin to update sensor image

Post by ycahome »

then, something is going wrong with me.

Tested in 2 different domoticz systems (Ubuntu Version: 3.8153 and raspberry Version: 3.8153) ----possible problem in that version???????

Seems that everything in the code is written as expected, check bellow (and icons are attached):


Can you test it and tell me what is the behavior to your installation?

Code: Select all


"""
<plugin key="MoonPhases" name="Moon Phases" author=" " version="1.0.0" wikilink="http://www.domoticz.com/wiki/plugins/" externallink=" ">
    <params>
        <param field="Mode1" label="Key WU" width="200px" required="true" default=""/>
        <param field="Mode2" label="Code Country" width="100px" required="true" default="fr"/>
        <param field="Mode3" label="City" width="300px" required="true" default="paris"/>
      <param field="Mode4" label="Polling interval (minutes, 30 mini)" width="40px" required="true" default="2"/>
        <param field="Mode6" label="Debug" width="75px">
            <options>
                <option label="True" value="Debug"/>
                <option label="False" value="Normal"  default="True" />
            </options>
        </param>
    </params>
</plugin>
"""

import Domoticz
# import urllib
# from urllib.request import request, urlopen
import urllib.request
import json
from datetime import datetime
from datetime import timedelta

icons = {"NewMoon": "NewMoon.zip",
         "WaxingCrescent": "WaxingCrescent.zip",
         "FirstQuarter": "FirstQuarter.zip",
         "WaxingGibbous": "WaxingGibbous.zip",
         "Full": "Full.zip",
         "WaningGibbous": "WaningGibbous.zip",
         "LastQuarter": "LastQuarter.zip",
         "WaningCrescent": "WaningCrescent.zip"}




class BasePlugin:
  
    def __init__(self):
        self.debug = False
        self.nextupdate = datetime.now()
        self.pollinterval = 60  # default polling interval in minutes
        self.error = False
        return

    def onStart(self):
        global icons
        Domoticz.Debug("onStart called")
        if Parameters["Mode6"] == 'Debug':
            self.debug = True
            Domoticz.Debugging(1)
            DumpConfigToLog()
        else:
            Domoticz.Debugging(0)

        # load custom MoonPhase images
        for key, value in icons.items():
            if key not in Images:
                Domoticz.Image(value).Create()
                Domoticz.Debug("Added icon: " + key + " from file " + value)
        Domoticz.Debug("Number of icons loaded = " + str(len(Images)))
        for image in Images:
            Domoticz.Debug("Icon " + str(Images[image].ID) + " " + Images[image].Name)


        # create the mandatory child device if it does not yet exist
        if 1 not in Devices:
            Domoticz.Device(Name="Lune", Unit=1, TypeName="Custom",Options={"Custom": "1;"},Used=1).Create()

        # check polling interval parameter
        try:
            temp = int(Parameters["Mode4"])
        except:
            Domoticz.Error("Invalid polling interval parameter")
        else:
            if temp < 1:
                temp = 1  # minimum polling interval
                Domoticz.Error("Specified polling interval too short: changed to 30 minutes")
            elif temp > 1440:
                temp = 1440  # maximum polling interval is 1 day
                Domoticz.Error("Specified polling interval too long: changed to 1440 minutes (24 hours)")
            self.pollinterval = temp
        Domoticz.Log("Using polling interval of {} minutes".format(str(self.pollinterval)))

    def onStop(self):
        Domoticz.Debug("onStop called")
        Domoticz.Debugging(0)

    def onHeartbeat(self):
        now = datetime.now()
        if now >= self.nextupdate:
            self.nextupdate = now + timedelta(minutes=self.pollinterval)
            # data=json.loads(urllib.request.urlopen("http://api.wunderground.com/api/5ea448da70b1136f/astronomy/q/fr/paris.json").read().decode('ascii'))
            data = json.loads(urllib.request.urlopen(
                "http://api.wunderground.com/api/" + Parameters["Mode1"] + "/astronomy/q/" + Parameters["Mode2"] + "/" +
                Parameters["Mode3"] + ".json").read().decode('ascii'))
            lune = data['moon_phase']['phaseofMoon']
			# Domoticz.Log(str(lune))
            Domoticz.Log("Lune Data:"+str(lune))
            self.UpdateDevice(lune)



    def UpdateDevice(self, lune):
        # Make sure that the Domoticz device still exists (they can be deleted) before updating it
        if 1 in Devices:
            if lune == "New Moon":
                icon = "NewMoon"
                datafr = "New Moon"
            elif lune == "Waxing Crescent":
                icon = "WaxingCrescent"
                datafr = "Waxing Crescent"
            elif lune == "First Quarter":
                icon = "FirstQuarter"
                datafr = "First Quarter"
            elif lune == "Waxing Gibbous":
                icon = "WaxingGibbous"
                datafr = "Waxing Gibbous"
            elif lune == "Full":
                icon = "Full"
                datafr = "Full"
            elif lune == "Waning Gibbous":
                icon = "WaningGibbous"
                datafr = "Waning Gibbous"
            elif lune == "Last Quarter":
                icon = "LastQuarter"
                datafr = "Last Quarter"
            elif lune == "WaningCrescent":
                icon = "WaningCrescent"
                datafr = "WaningCrescent"


            #Domoticz.Debug("Icon " + str(Images[icon].ID))
            Devices[1].Update(0, str(lune), Image=Images[icon].ID)

        return

global _plugin
_plugin = BasePlugin()

def onStart():
    global _plugin
    _plugin.onStart()

def onStop():
    global _plugin
    _plugin.onStop()

def onHeartbeat():
    global _plugin
    _plugin.onHeartbeat()

# Generic helper functions
def DumpConfigToLog():
    for x in Parameters:
        if Parameters[x] != "":
            Domoticz.Debug("'" + x + "':'" + str(Parameters[x]) + "'")
    Domoticz.Debug("Device count: " + str(len(Devices)))
    for x in Devices:
        Domoticz.Debug("Device:           " + str(x) + " - " + str(Devices[x]))
        Domoticz.Debug("Device ID:       '" + str(Devices[x].ID) + "'")
        Domoticz.Debug("Device Name:     '" + Devices[x].Name + "'")
        Domoticz.Debug("Device nValue:    " + str(Devices[x].nValue))
        Domoticz.Debug("Device sValue:   '" + Devices[x].sValue + "'")
    return



The symptom is: The first time that domoticz creates those icons everything goes well. On restart, or plugin update, adds only one image to the array. But icons are available via domoticz console

Code: Select all

 2017-09-28 14:25:28.603 (MoonPhases) Creating images from file '/home/pi/domoticz/plugins/MoonPhases/WaningGibbous.zip'.
2017-09-28 14:25:28.616 (MoonPhases) Added icon: WaningGibbous from file WaningGibbous.zip
2017-09-28 14:25:28.616 (MoonPhases) Creating images from file '/home/pi/domoticz/plugins/MoonPhases/WaxingCrescent.zip'.
2017-09-28 14:25:28.627 (MoonPhases) Added icon: WaxingCrescent from file WaxingCrescent.zip
2017-09-28 14:25:28.627 (MoonPhases) Creating images from file '/home/pi/domoticz/plugins/MoonPhases/FirstQuarter.zip'.
2017-09-28 14:25:28.638 (MoonPhases) Added icon: FirstQuarter from file FirstQuarter.zip
2017-09-28 14:25:28.638 (MoonPhases) Creating images from file '/home/pi/domoticz/plugins/MoonPhases/WaningCrescent.zip'.
2017-09-28 14:25:28.650 (MoonPhases) Added icon: WaningCrescent from file WaningCrescent.zip
2017-09-28 14:25:28.650 (MoonPhases) Creating images from file '/home/pi/domoticz/plugins/MoonPhases/WaxingGibbous.zip'.
2017-09-28 14:25:28.662 (MoonPhases) Added icon: WaxingGibbous from file WaxingGibbous.zip
2017-09-28 14:25:28.662 (MoonPhases) Creating images from file '/home/pi/domoticz/plugins/MoonPhases/LastQuarter.zip'.
2017-09-28 14:25:28.673 (MoonPhases) Added icon: LastQuarter from file LastQuarter.zip
2017-09-28 14:25:28.674 (MoonPhases) Creating images from file '/home/pi/domoticz/plugins/MoonPhases/Full.zip'.
2017-09-28 14:25:28.685 (MoonPhases) Added icon: Full from file Full.zip
2017-09-28 14:25:28.685 (MoonPhases) Number of icons loaded = 1
2017-09-28 14:25:28.685 (MoonPhases) Icon 108 NewMoon 
Attachments
MoonPhases.zip
(123.53 KiB) Downloaded 39 times
Last edited by ycahome on Tuesday 03 October 2017 10:57, edited 1 time in total.
zak45
Posts: 953
Joined: Sunday 22 January 2017 11:37
Target OS: Windows
Domoticz version: V2024.4
Contact:

Re: Use Domoticz.image from plugin to update sensor image

Post by zak45 »

Hi,
looks like the same, but don't know if this is normal or not ?? !!!

Code: Select all

 2017-09-28 17:32:28.168 (zz) Initialized version 1.0.0, author ' '
2017-09-28 17:32:28.168 (zz) Entering work loop.
2017-09-28 17:32:28.168 (zz) Debug log level set to: 'true'.
2017-09-28 17:32:28.168 (zz) 'Name':'zz'
2017-09-28 17:32:28.168 (zz) 'HardwareID':'19'
2017-09-28 17:32:28.168 (zz) 'Mode6':'Debug'
2017-09-28 17:32:28.168 (zz) 'Mode2':'fr'
2017-09-28 17:32:28.168 (zz) 'Port':'0'
2017-09-28 17:32:28.168 (zz) 'HomeFolder':'C:\Program Files (x86)\Domoticz\plugins\MoonPhases\'
2017-09-28 17:32:28.168 (zz) 'Key':'MoonPhases'
2017-09-28 17:32:28.168 (zz) 'Mode4':'2'
2017-09-28 17:32:28.168 (zz) 'Version':'1.0.0'
2017-09-28 17:32:28.168 (zz) 'Mode1':'5ea448da70b1136f'
2017-09-28 17:32:28.168 (zz) 'Mode3':'paris'
2017-09-28 17:32:28.168 (zz) 'Author':' '
2017-09-28 17:32:28.168 (zz) Device count: 1
2017-09-28 17:32:28.168 (zz) Device: 1 - ID: 937, Name: 'zz - Lune', nValue: 0, sValue: 'First Quarter'
2017-09-28 17:32:28.168 (zz) Device ID: '937'
2017-09-28 17:32:28.168 (zz) Device Name: 'zz - Lune'
2017-09-28 17:32:28.168 (zz) Device nValue: 0
2017-09-28 17:32:28.168 (zz) Device sValue: 'First Quarter'
2017-09-28 17:32:28.168 (zz) Creating images from file 'C:\Program Files (x86)\Domoticz\plugins\MoonPhases\WaningGibbous.zip'.
2017-09-28 17:32:28.215 (zz) Added icon: WaningGibbous from file WaningGibbous.zip
2017-09-28 17:32:28.215 (zz) Creating images from file 'C:\Program Files (x86)\Domoticz\plugins\MoonPhases\FirstQuarter.zip'.
2017-09-28 17:32:28.278 (zz) Added icon: FirstQuarter from file FirstQuarter.zip
2017-09-28 17:32:28.278 (zz) Creating images from file 'C:\Program Files (x86)\Domoticz\plugins\MoonPhases\NewMoon.zip'.
2017-09-28 17:32:28.324 (zz) Added icon: NewMoon from file NewMoon.zip
2017-09-28 17:32:28.324 (zz) Creating images from file 'C:\Program Files (x86)\Domoticz\plugins\MoonPhases\LastQuarter.zip'.
2017-09-28 17:32:28.387 (zz) Added icon: LastQuarter from file LastQuarter.zip
2017-09-28 17:32:28.387 (zz) Creating images from file 'C:\Program Files (x86)\Domoticz\plugins\MoonPhases\WaxingGibbous.zip'.
2017-09-28 17:32:28.449 (zz) Added icon: WaxingGibbous from file WaxingGibbous.zip
2017-09-28 17:32:28.449 (zz) Creating images from file 'C:\Program Files (x86)\Domoticz\plugins\MoonPhases\WaxingCrescent.zip'.
2017-09-28 17:32:28.512 (zz) Added icon: WaxingCrescent from file WaxingCrescent.zip
2017-09-28 17:32:28.512 (zz) Creating images from file 'C:\Program Files (x86)\Domoticz\plugins\MoonPhases\WaningCrescent.zip'.
2017-09-28 17:32:28.559 (zz) Added icon: WaningCrescent from file WaningCrescent.zip
2017-09-28 17:32:28.559 (zz) Number of icons loaded = 1
2017-09-28 17:32:28.559 (zz) Icon 115 Full
2017-09-28 17:32:28.559 (zz) Using polling interval of 2 minutes
2017-09-28 17:32:37.762 (zz) Calling message handler 'onHeartbeat'.
2017-09-28 17:32:38.044 (zz) Lune Data:First Quarter
2017-09-28 17:32:38.044 Error: (zz) 'onHeartbeat' failed 'KeyError'.
2017-09-28 17:32:38.059 Error: (zz) ----> Line 152 in C:\Program Files (x86)\Domoticz\plugins\MoonPhases\plugin.py, function onHeartbeat
2017-09-28 17:32:38.059 Error: (zz) ----> Line 101 in C:\Program Files (x86)\Domoticz\plugins\MoonPhases\plugin.py, function onHeartbeat
2017-09-28 17:32:38.059 Error: (zz) ----> Line 135 in C:\Program Files (x86)\Domoticz\plugins\MoonPhases\plugin.py, function UpdateDevice
2017-09-28 17:32:47.935 (zz) Calling message handler 'onHeartbeat'.
2017-09-28 17:32:49.310 (RFX433E) Energy (Compteur EDF) 
User avatar
ycahome
Posts: 248
Joined: Sunday 12 February 2017 10:55
Target OS: Linux
Domoticz version: lat Beta
Contact:

Re: Use Domoticz.image from plugin to update sensor image

Post by ycahome »

Can someone from the dev team of the python framework answer this ?
Is it a problem to our installations or a bug?

Also, tried to contact the developer of the z-wave Battery plugin (Logread) but seems that his mailbox is full.
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Use Domoticz.image from plugin to update sensor image

Post by Logread »

ycahome wrote: Monday 02 October 2017 10:43 Also, tried to contact the developer of the z-wave Battery plugin (Logread) but seems that his mailbox is full.
I got your pm ;)
Pretty tied up right now but I’ll look into what you posted earlier in the thread as soon as possible... Icons files can be very tricky to get to work (espcially the plugin key must match in all the icons.txt files. Bear with me please
User avatar
ycahome
Posts: 248
Joined: Sunday 12 February 2017 10:55
Target OS: Linux
Domoticz version: lat Beta
Contact:

Re: Use Domoticz.image from plugin to update sensor image

Post by ycahome »

Logread wrote: Monday 02 October 2017 19:31 Bear with me please
Nice. ;-)

In the meantime I have tried with different sets of ready made icons (from domoticz repository) without success.
Even tried with your plugin modified.


Domoticz.image seems poorly documented.
Especially for the "icons.txt" file, the wiki pages have conflicting articles. Sometimes documentation say "name;plugin name;description" while others "name;name;description".

My latest finding is that image array keeps only the last image created, all previously added images were removed. (Although all images were actually added to domoticz console).
User avatar
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Cannot Use Domoticz.image from plugin to update sensor image

Post by Dnpwwo »

@ycahome,

The plugin doc (http://www.domoticz.com/wiki/Developing ... gin#Images) tries to show that the framework maps the images to the plugin using the plugin's unique key. Because the Python framework is on top of existing Domoticz somethings are not as perfect as they might be if designed from scratch and this is an example.

If your plugin key is "MoonPhases" then icons.txt needs to refer to that so that Domoticz knows which images to load.

Rather than:

Code: Select all

WaningGibbous;WaningGibbous;WaningGibbous MoonPhases
try:

Code: Select all

MoonPhasesWaningGibbous;WaningGibbous;WaningGibbous MoonPhases
or:

Code: Select all

MoonPhasesWG;WaningGibbous;WaningGibbous MoonPhases
and the Images will show up in the Images dict and the image number will be available.

The Device.Update will set the LastSeen on the device as well so the Domoticz front end should refresh as long as th type of device supports custom images.
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
User avatar
ycahome
Posts: 248
Joined: Sunday 12 February 2017 10:55
Target OS: Linux
Domoticz version: lat Beta
Contact:

Re: Cannot Use Domoticz.image from plugin to update sensor image

Post by ycahome »

Dnpwwo wrote: Tuesday 03 October 2017 3:36 @ycahome,

The plugin doc (http://www.domoticz.com/wiki/Developing ... gin#Images) tries to show that the framework maps the images to the plugin using the plugin's unique key. Because the Python framework is on top of existing Domoticz somethings are not as perfect as they might be if designed from scratch and this is an example.

If your plugin key is "MoonPhases" then icons.txt needs to refer to that so that Domoticz knows which images to load.

Rather than:

Code: Select all

WaningGibbous;WaningGibbous;WaningGibbous MoonPhases
try:

Code: Select all

MoonPhasesWaningGibbous;WaningGibbous;WaningGibbous MoonPhases
or:

Code: Select all

MoonPhasesWG;WaningGibbous;WaningGibbous MoonPhases
and the Images will show up in the Images dict and the image number will be available.

The Device.Update will set the LastSeen on the device as well so the Domoticz front end should refresh as long as th type of device supports custom images.
MANY THANKS dnpwwo for this clarification. ;-)

Solution was too simple for the effort given by myself.

Thought that where just examples and that there is no relation between "icons.txt" contents and the plugin key (which is obvious wrong assumption) :-).

Maybe its a good opportunity to make a distinct Wiki note:

NOTE:"icons.txt" file contents should be prefixed with your plugin key
example:
if your plugin key is "myPlugin" your "icons.txt" file may look like :

Code: Select all

myPluginIcon1;Icon1Name;Plugin Description
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest