Python Plugin: MqttMapper

Python and python framework

Moderator: leecollings

WBeulink
Posts: 23
Joined: Monday 21 December 2020 18:29
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: NL
Contact:

Re: Python Plugin: MqttMapper

Post by WBeulink »

Confirm. Works! Thanks.
So ~0 is third item of svalue in DZ?
As expected by this device in DZ?

Willem
--
RP4 - Buster/BullsEye - Domoticz 2023.2 - Z-Wave JS UI
Willem
--
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Python Plugin: MqttMapper

Post by FlyingDomotic »

Yep, device needs sValue=temperature;humidity;status
Status could be one of: 0=Normal, 1=Comfortable, 2=Dry, 3=Wet
Details at https://www.domoticz.com/wiki/Domoticz_ ... 2Fhumidity
WBeulink
Posts: 23
Joined: Monday 21 December 2020 18:29
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: NL
Contact:

Re: Python Plugin: MqttMapper

Post by WBeulink »

I'am trying to get a switch in a remote domoticz install to function via MQTT in an other domoticz install.
So listening to the remote MQTT-messages I get these 2 json's when I switch off and on in the remote ....

============================================
{
"Battery": 255,
"LastUpdate": "2023-08-11 13:59:59",
"RSSI": 12,
"description": "",
"dtype": "Light/Switch",
"hwid": "24",
"id": "shellyplug-s-EF6165-0",
"idx": 1152,
"name": "5.2 TestShellyPlug",
"nvalue": 0,
"stype": "Switch",
"svalue1": "Off",
"switchType": "On/Off",
"unit": 5
}
============================================
{
"Battery": 255,
"LastUpdate": "2023-08-11 14:01:17",
"RSSI": 12,
"description": "",
"dtype": "Light/Switch",
"hwid": "24",
"id": "shellyplug-s-EF6165-0",
"idx": 1152,
"name": "5.2 TestShellyPlug",
"nvalue": 1,
"stype": "Switch",
"svalue1": "On",
"switchType": "On/Off",
"unit": 5
}
============================================

So it's both "nvalue" and "svalue1" changing in the json when switch is operated.

What would be the correct part in MqttMapper.json to make the new created device/switch function as wanted (as a switch)?
Part below doesn't work and throws erors ......

"5.2 TestShellyPlug": {
"topic": "domoticz/out/5.2 TestShellyPlug",
"type": "244", "subtype": "62", "switchtype": "0",
"set": {"payload": {"nvalue": "#"}},
"mapping": {"item": "nvalue", "default": "0", "values": {"Off": "0", "On": "1"}}
},

Willem
--
RP4 - Buster/BullsEye - Domoticz 2023.2 - Z-Wave JS UI
Willem
--
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Python Plugin: MqttMapper

Post by FlyingDomotic »

For an on/off switch, nValue is sufficient.

The wrong assumption is about the set part. You can't set domoticz/out to set a (remote) value. You should make a (more or less) complete message on domoticz/in topic, or use http API.

So, in any case, you have to remove the set part.

Next step depends on your need to set the remote switch from the local Domoticz. If you only need to get the (remote) state, that's over.

If you also want to set it from the local Domoticz, then you have to create a local push button to turn the remote switch on, probably using Domoticz http API, and another (local) one to turn it off.

It's a good idea to separate "state" device and "command" device, to avoid endless loop (you change state from local, which is sent to remote, remote sends a change on domoticz/out, which changes the local state, which is sent to remote ...)
WBeulink
Posts: 23
Joined: Monday 21 December 2020 18:29
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: NL
Contact:

Re: Python Plugin: MqttMapper

Post by WBeulink »

Thanks for your explanation.
I understand the misconcept I tried to build (sending to domoticz/out).
So I have to build something else.

But still .... if I only want to read (and see) the status of the remote switch .....

"5.2 TestShellyPlug": {
"topic": "domoticz/out/5.2 TestShellyPlug",
"type": "244", "subtype": "62", "switchtype": "0",
"mapping": {"item": "nvalue", "default": "0", "values": {"Off": "0", "On": "1"}}
},

doesn't work.
New mistakes made?
Willem
--
RP4 - Buster/BullsEye - Domoticz 2023.2 - Z-Wave JS UI
Willem
--
User avatar
waltervl
Posts: 5153
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Python Plugin: MqttMapper

Post by waltervl »

WBeulink wrote: Friday 11 August 2023 16:26 Thanks for your explanation.
I understand the misconcept I tried to build (sending to domoticz/out).
If you want to switch a switch on another domoticz server you can use action scripts. Instead of the bash script as example below use the http json API command eg for the On Action use (change IP, Port and IDX to real values)

Code: Select all

http://IPofDomoticz2:Port/json.htm?type=command&param=switchlight&idx=IDX&switchcmd=On
Image
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
WBeulink
Posts: 23
Joined: Monday 21 December 2020 18:29
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: NL
Contact:

Re: Python Plugin: MqttMapper

Post by WBeulink »

Thanks again for this part.
Will add it to my knowledge base.

For the part of switch (above) I decided to talk MQTT directly (because it's also MQTT-enabled) to it.
And not in the strange way via domoticz/out of my other Domoticz.

So now I have functioning:

"TestShellyPlug": {
"topic": "shellies/shellyplug-s-EF6165/relay/0",
"type": "244", "subtype": "73", "switchtype": "0",
"set": {"topic": "shellies/shellyplug-s-EF6165/relay/0/command", "payload": "#"},
"mapping": {"item": "", "default": "", "values": {"on": "100", "off": "0"}}
},
MQTT-mapper is connected to the other broker as you understand.

Why this?
I want to keep my Thermostat-RPi-Domoticz clean.
And keep all power-metering on my Energy-RPi-Domoticz.
Now I can switch things off/on from the first while keeping the switch and power meters itself talking to the second.
So my shelly-plugin is only needed on the last one.

And more ...
It's interesting to experiment with your mapper-plugin!
Well done!

Willem
--
RP4 - Buster/BullsEye - Domoticz 2023.2 - Z-Wave JS UI
Willem
--
User avatar
waltervl
Posts: 5153
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Python Plugin: MqttMapper

Post by waltervl »

A little bit off topic (and perhaps I asked this before): why all these seperate Domoticz instances? You can relay the energy data directly to your main RPi,....
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Python Plugin: MqttMapper

Post by FlyingDomotic »

WBeulink wrote: Friday 11 August 2023 16:26
"5.2 TestShellyPlug": {
"topic": "domoticz/out/5.2 TestShellyPlug",
"type": "244", "subtype": "62", "switchtype": "0",
"mapping": {"item": "nvalue", "default": "0", "values": {"Off": "0", "On": "1"}}
},

doesn't work.
What do you mean by "doesn't work"? Any error messages (which ones?), just do nothing, ...?
User avatar
waltervl
Posts: 5153
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Python Plugin: MqttMapper

Post by waltervl »

Perhaps usefull to share: In latest beta a general setpoint device is introduced based on the old thermostat device viewtopic.php?p=307044#p307044
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Python Plugin: MqttMapper

Post by FlyingDomotic »

Thank's!
candymirror
Posts: 5
Joined: Thursday 18 January 2018 15:32
Target OS: Linux
Domoticz version:
Contact:

Re: Python Plugin: MqttMapper

Post by candymirror »

I'm trying to add a dimmer, but though I'm able to set de dim level of the device, MqttMapper can't map a received level.
Here's my json entry:
"set-speed": {"topic": "fan/cv_booster/speed_level/state",
"type": "244", "subtype": "73", "switchtype": "7",
"set": {"topic": "fan/cv_booster/speed_level/command", "payload": "#"},
"mapping": {"item": ""}
}

setting the level manual trough a mqqt sub command, will also send a new message in de ../state topic with a value from 0 to 100. But this value can't be mapped to de domoticz device:
Bad mapping for ...

Setting the level on de domoticz device towards de mqtt device on the ../command topic works very well.

I solved this Quick 'n Dirty for now by adding "valueToSet = readValue" right within the "else" of the "Bad mapping" error exception.

Maybe someone can point me in right direction to solve it correctly.
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Python Plugin: MqttMapper

Post by FlyingDomotic »

Code was not able to support dimmer. This just changed with last version, available since few minutes.
Could you update and try it?
TIA.
candymirror
Posts: 5
Joined: Thursday 18 January 2018 15:32
Target OS: Linux
Domoticz version:
Contact:

Re: Python Plugin: MqttMapper

Post by candymirror »

Thanks for the effort, but the new code results in an error:

Code: Select all

2024-01-24 13:54:00.653 Error: Radiator-DBE: Traceback (most recent call last):
2024-01-24 13:54:00.653 Error: Radiator-DBE: File "/opt/domoticz/userdata/plugins/MqttMapper/plugin.py", line 543, in onMessage
2024-01-24 13:54:00.653 Error: Radiator-DBE: _plugin.onMessage(Connection, Data)
2024-01-24 13:54:00.653 Error: Radiator-DBE: File "/opt/domoticz/userdata/plugins/MqttMapper/plugin.py", line 288, in onMessage
2024-01-24 13:54:00.653 Error: Radiator-DBE: self.mqttClient.onMessage(Connection, Data)
2024-01-24 13:54:00.653 Error: Radiator-DBE: File "/opt/domoticz/userdata/plugins/MqttMapper/plugin.py", line 149, in onMessage
2024-01-24 13:54:00.653 Error: Radiator-DBE: self.mqttPublishCb(topic, Data['Payload'])
2024-01-24 13:54:00.653 Error: Radiator-DBE: File "/opt/domoticz/userdata/plugins/MqttMapper/plugin.py", line 390, in onMQTTPublish
2024-01-24 13:54:00.653 Error: Radiator-DBE: Domoticz.Log('Setting '+device.Name+' to '+str(nValueToSet)+'/'+sValueToSet) # Value is numeric
2024-01-24 13:54:00.653 Error: Radiator-DBE: UnboundLocalError: local variable 'nValueToSet' referenced before assignment
2024-01-24 13:54:50.637 Error: Radiator-DBE: Call to function 'onMessage' failed, exception details:
I'm not a programmer, but it seems to be hanging in this part of your code:

Code: Select all

                        if nodeType == '244':   # This is a switch
                            if nodeSwitchtype == '0': # This is an On/Off switch
                                nValueToSet = 0 if str(valueToSet) == '0' else 1
                            sValueToSet = str(valueToSet)
My device is identifying itself as type =244 and switchtype=7. So "nValueToSet" isn't declared or referenced before being used in line 390: Domoticz.Log('Setting '+device.Name+' to '+str(nValueToSet)+'/'+sValueToSet) # Value is numeric
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Python Plugin: MqttMapper

Post by FlyingDomotic »

Let's try with last version.

Note: you're not a programmer, but you found where the problem was ;-) In fact, nValueToSet was effectively not set if switch was not an on/off one.
candymirror
Posts: 5
Joined: Thursday 18 January 2018 15:32
Target OS: Linux
Domoticz version:
Contact:

Re: Python Plugin: MqttMapper

Post by candymirror »

Thanks FlyingDomotic,

This works!

BTW. I'm more of a wannabe programmer, I do know my way around in making small shell scripts. But if it gets any bigger, every little change or added code is build upon trial an error (and based upon code from others).
Superpjeter
Posts: 11
Joined: Wednesday 29 August 2018 14:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MqttMapper

Post by Superpjeter »

I made a device with a setpoint and I can change the setpoint manually

Code: Select all

   "HBVK - Ventilator snelheid ref": {
        "topic": "Voorkamer/fan-speed-ref",
        "options": {"ValueMin":"0","ValueMax":"100", "ValueUnit":"%"},
        "type": "242", "subtype": "1", "switchtype": "0",
        "set": {"topic": "Voorkamer/fan-speed-ref"},
        "mapping": {"item": ""}
    },
But if I change the setpoint (50) in a dzvents script the ondevicemodified is triggered and the old value (79) is send
2024-02-02 22:15:00.237 HeatBooster: 004/HBVK - Ventilator snelheid ref, Voorkamer/fan-speed-ref: Command: 'Set Level', Level: 50.0, Color:
2024-02-02 22:15:00.237 HeatBooster: Setting Voorkamer/fan-speed-ref to >50.0<
2024-02-02 22:15:00.237 HeatBooster: MqttClient::Publish Voorkamer/fan-speed-ref (50.0)
2024-02-02 22:15:00.240 HeatBooster: onDeviceModified 004/HBVK - Ventilator snelheid ref, Voorkamer/fan-speed-ref, sValue=79.0
2024-02-02 22:15:00.240 HeatBooster: Setting Voorkamer/fan-speed-ref to >79.0<
2024-02-02 22:15:00.240 HeatBooster: MqttClient::Publish Voorkamer/fan-speed-ref (79.0)
How can I prevent that the old value is send?
FlyingDomotic
Posts: 303
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Python Plugin: MqttMapper

Post by FlyingDomotic »

Hello!

I just copied/pasted your definition in a json file, and tried it.

I tested by changing target temp from 3.0 to 4.0 with GUI, and then to 19.0 by a LUA script. Here's the log:

Code: Select all

2024-02-04 16:48:00.144  Test MQTT: 001/Test MQTT - HBVK - Ventilator snelheid ref, Voorkamer/fan-speed-ref: Command: 'Set Level', Level: 4.0, Color:
2024-02-04 16:48:00.144  Test MQTT: Setting Voorkamer/fan-speed-ref to >4.0<
2024-02-04 16:48:00.145  Test MQTT: MqttClient::Publish Voorkamer/fan-speed-ref (4.0)
2024-02-04 16:48:00.195  Test MQTT: onMQTTConnected found Voorkamer/fan-speed-ref, Device 'Test MQTT - HBVK - Ventilator snelheid ref'
2024-02-04 16:48:00.195  Test MQTT: Setting Test MQTT - HBVK - Ventilator snelheid ref to >4.0<
2024-02-04 16:48:00.195  (Test MQTT - HBVK - Ventilator snelheid ref) Updating device from 0:'3.0' to have values 0:'4.0'.
2024-02-04 16:48:00.201  Status: LUA: Device based event fired on 'Test MQTT - HBVK - Ventilator snelheid ref', value '4.0'
2024-02-04 16:48:00.201  Status: LUA: Device based event fired on 'Test MQTT - HBVK - Ventilator snelheid ref_Utility', value '4.0'
2024-02-04 16:50:08.575  Status: EventSystem: reset all events...
2024-02-04 16:50:15.114  Status: User: Admin (IP: 192.168.159.134) initiated a switch command (352/Test/On)
2024-02-04 16:50:15.122  Status: LUA: Device based event fired on 'Test', value 'On'
2024-02-04 16:50:15.123  Dummy devices: Light/Switch (Test)
2024-02-04 16:50:15.666  Test MQTT: 001/Test MQTT - HBVK - Ventilator snelheid ref, Voorkamer/fan-speed-ref: Command: 'Set Level', Level: 19.0, Color:
2024-02-04 16:50:15.666  Test MQTT: Setting Voorkamer/fan-speed-ref to >19.0<
2024-02-04 16:50:15.666  Test MQTT: MqttClient::Publish Voorkamer/fan-speed-ref (19.0)
2024-02-04 16:50:15.718  Test MQTT: onMQTTConnected found Voorkamer/fan-speed-ref, Device 'Test MQTT - HBVK - Ventilator snelheid ref'
2024-02-04 16:50:15.718  Test MQTT: Setting Test MQTT - HBVK - Ventilator snelheid ref to >19.0<
2024-02-04 16:50:15.718  (Test MQTT - HBVK - Ventilator snelheid ref) Updating device from 0:'4.0' to have values 0:'19.0'.
2024-02-04 16:50:15.726  Status: LUA: Device based event fired on 'Test MQTT - HBVK - Ventilator snelheid ref_Utility', value '19.0'
2024-02-04 16:50:15.726  Status: LUA: Device based event fired on 'Test MQTT - HBVK - Ventilator snelheid ref', value '19.0'
AS you can see, I don't have the "onDeviceModified" you got. How do you set the target temperature?
Superpjeter
Posts: 11
Joined: Wednesday 29 August 2018 14:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MqttMapper

Post by Superpjeter »

I see that you use LUA script, so I tried that also and that is indeed working fine.
The dzvents script I use:

Code: Select all

return {
	on = {
		timer = {
			'every minute',
		}
	},
	execute = function(domoticz, timer)
		domoticz.devices(183).updateSetPoint(25)
	end
}
May be a Domoticz issue?
Superpjeter
Posts: 11
Joined: Wednesday 29 August 2018 14:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MqttMapper

Post by Superpjeter »

However when I use it with another setpoint device not related to MqttMapper the dzvents script is working fine
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest