Python Plugin: MqttMapper

Python and python framework

Moderator: leecollings

hjzwiers
Posts: 215
Joined: Friday 12 January 2018 8:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MqttMapper

Post by hjzwiers »

I forgot the { ...

It is running
FlyingDomotic
Posts: 349
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Python Plugin: MqttMapper

Post by FlyingDomotic »

Mapping values are made for you ;-)
Let's try:

Code: Select all

"type": "244", "subtype": "73", "switchtype": "0",
"mapping": {"item": "", "default": "0", "values": {"True": "1"}}
hjzwiers
Posts: 215
Joined: Friday 12 January 2018 8:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MqttMapper

Post by hjzwiers »

I tried your alteration suggestion, it generated an "MQTT mapper: Error reading JSON file /home/hjz/domoticz/plugins/MqttMapper/MqttMapper.json" error.

I reloaded my script, and it now generated an error as well, what changed? (both were run after restarting domoticz)

"MQTTrunning": {
"topic": "ebusd/global/running",
"type": "243", "subtype": "19", "switchtype": "0",
"mapping": {"item": ""}
#"type": "244", "subtype": "73", "switchtype": "0",
#"mapping": {"item": "", "default": "0", "values": {"True": "1"}}
}
Last edited by hjzwiers on Wednesday 03 April 2024 10:27, edited 1 time in total.
FlyingDomotic
Posts: 349
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Python Plugin: MqttMapper

Post by FlyingDomotic »

Would it be possible to get the full configuration file?

In addition, upgrading to latest version will give more details about JSON error (this is probably a syntax error).
hjzwiers
Posts: 215
Joined: Friday 12 January 2018 8:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MqttMapper

Post by hjzwiers »

How do I get the full configuration file, I am happy to send it to you
FlyingDomotic
Posts: 349
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Python Plugin: MqttMapper

Post by FlyingDomotic »

On Domoticz hardware page, select MQTT Mapper plugin and read the configuration file name on the "JSON mapping file to use:" label line.
Unless you changed it, it shoud be "MqttMapper.json".
The file is located on Domoticz plugin folder, in MqttMapper sub-folder (by default "/home/pi/domoticz/plugin/MqttMapper/MqttMapper.json")
hjzwiers
Posts: 215
Joined: Friday 12 January 2018 8:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MqttMapper

Post by hjzwiers »

{
"T_Outside": {
"topic": "ebusd/broadcast/outsidetemp",
"type": "80", "subtype": "5", "switchtype": "0",
"mapping": {"item": "", "multiplier": 0.1}
},

"T_Living": {
"topic": "ebusd/ctlv2/z1RoomTemp",
"type": "80", "subtype": "5", "switchtype": "0",
"mapping": {"item": "", "multiplier": 0.1}
},

"MQTTrunning": {
"topic": "ebusd/global/running",
"type": "243", "subtype": "19", "switchtype": "0",
"mapping": {"item": ""}
#"type": "244", "subtype": "73", "switchtype": "0",
#"mapping": {"item": "", "default": "0", "values": {"True": "1"}}
}

}

This one?
FlyingDomotic
Posts: 349
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Python Plugin: MqttMapper

Post by FlyingDomotic »

Yes, that's the right one.
Let's try this way:

Code: Select all

{
	"T_Outside": {
		"topic": "ebusd/broadcast/outsidetemp",
		"type": "80", "subtype": "5", "switchtype": "0",
		"mapping": {"item": "", "multiplier": 0.1}
	},

	"T_Living": {
		"topic": "ebusd/ctlv2/z1RoomTemp",
		"type": "80", "subtype": "5", "switchtype": "0",
		"mapping": {"item": "", "multiplier": 0.1}
	},

	"MQTTrunning": {
		"topic": "ebusd/global/running",
		"type": "244", "subtype": "73", "switchtype": "0",
		"mapping": {"item": "", "default": "0", "values": {"True": "1"}}
	}
}
FYI, there's no comment in JSON (which is not the best idea its designers had), so lines with a # are not understood and generates an error.
hjzwiers
Posts: 215
Joined: Friday 12 January 2018 8:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MqttMapper

Post by hjzwiers »

I took the # lines out and it worked again. Sorry about that (want to make it easy to change)

I have a question if the value is false, what does it do? When I restarted I saw that my script wasn't showing a false. So I want to try but maybe in advance check the false statement.

Thanks in advance
FlyingDomotic
Posts: 349
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: Select all

"mapping": {"item": "", "default": "0", "values": {"True": "1"}}
means: "True"=1, default (all other values) is zero.
Value is evaluated each time a MQTT message on that topic is received (so you have to wait first MQTT update to see the right value in Domoticz, unless topic is retained).
hjzwiers
Posts: 215
Joined: Friday 12 January 2018 8:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MqttMapper

Post by hjzwiers »

Ran and tested, it works fine. Thanks!
FlyingDomotic
Posts: 349
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Python Plugin: MqttMapper

Post by FlyingDomotic »

We're never protected against chance ;-)
tarmacalastair
Posts: 95
Joined: Wednesday 30 October 2019 14:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Great Britain
Contact:

Re: Python Plugin: MqttMapper

Post by tarmacalastair »

Hello,

I wonder if you could help me with what I need to do for my UK smart meters please? Here we have very dumb smart meters which can't be queried directly by the users unless you buy an additional consumer access device (CAD). One supplier of these is Hildebrand with their platform and app called Bright.

At the moment their CADs are out of stock. However, the readings are sent to a national collection company, DCC every half hour and Hildebrand provides an API to interrogate this and some very helpful people have written python scripts to collect these readings:
https://github.com/cybermaggedon/pyglowmarkt and
https://github.com/danmed/Glow2MQTT

Using Glow2MQTT (via a crontab job every 20 minutes or so) I am able to retrieve 4 separate items - cost for electric and gas and consumption for each and I can see them in MQTT Explorer:
Screenshot from 2024-04-15 13-41-31.png
Screenshot from 2024-04-15 13-41-31.png (28.09 KiB) Viewed 1339 times
What I'm struggling with now, is which device types to use and how to format them in MqqtMapper to get them to display nicely in Domoticz. I think that all the counters are cumulative, starting at zero at midnight so ideally I'd like to retrieve the figures into a counter so I can view the Domoticz graphs to follow consumption by the hour/day/month, etc in KWh and the cost in pence or GB £.

I've looked at types 113, 243 (subtypes 28, 29 & 33) and types 248, 250 & 251 and I am just so confused. Sometimes the widget in Domoticz shows the value at the top right of the widget, sometimes in the middle, next to the description (sometimes with a zero at the top RHS), sometimes with a unit showing, sometimes not.

Any thoughts please to get me started?

Many thanks.
Running Domoticz and Dashticz beta on Raspberry Pi OS 32 bit - has anyone had success with 64 bit?

Desktop access from Ubuntu Linux
User avatar
waltervl
Posts: 5735
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Python Plugin: MqttMapper

Post by waltervl »

There is no Domoticz counter device that can handle a daily reset to 0. So you probably end up with a domoticz custom sensor that will just show the daily cumulative values. No decent reporting on this. Unless you do extra scripting but that is out of scope for this MQTT mapper plugin.

Better find a MQTT source that shows the absolute total cumulative value, so not resetting to 0 every day. Then Domoticz can do all the nice calculations and reporting for you out of the box.
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: 349
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Python Plugin: MqttMapper

Post by FlyingDomotic »

You may use "custom counter", it'll show current daily value, but does nothing with cumulative values.

However, you may still store values into any kind of device (including text), and do a small script to intercept each value change, to keep a "classical" power counter updated.

I use a similar script to dispatch a global power counter into day/night tariffs.
tarmacalastair
Posts: 95
Joined: Wednesday 30 October 2019 14:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Great Britain
Contact:

Re: Python Plugin: MqttMapper

Post by tarmacalastair »

Oh dear I was really excited and thought it would be so simple with a little bit of guidance.

Any idea how they do it in Home Assistant with these values? The readme for the Glow2MQTT script makes it look easy with great graphics (see attachment) but I really don't want to use home assistant as I've been using Domoticz for years and love it. But when I say using Domoticz, I use the GUi side of it extensively but have no idea how to script beyond making very minor modifications to other people's so really wouldn't know where to start. Can you give me any pointers?

Thanks.
Attachments
Screenshot 2024-04-15 at 23-27-39 Issues · danmed_Glow2MQTT.png
Screenshot 2024-04-15 at 23-27-39 Issues · danmed_Glow2MQTT.png (164.95 KiB) Viewed 1313 times
Running Domoticz and Dashticz beta on Raspberry Pi OS 32 bit - has anyone had success with 64 bit?

Desktop access from Ubuntu Linux
FlyingDomotic
Posts: 349
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Python Plugin: MqttMapper

Post by FlyingDomotic »

Idea is push data into a custom device. For example:

Code: Select all

{
	"Elec": {
		"topic": "glomarkt",
		"key": "elec",
		"type": "243", "subtype": "31", "switchtype": "0",
		"options": {"Custom":"1;Electricity"},
		"mapping": {"item": "elecconsumptiontoday"}
	}
}
Then, create a "standard" virtual counter (here, can be of type "counter"), name it "Counter".

Then, create a small script, activated when "Elec" is modified and :
- read "Elec" value
- compare it with previous (saved) value
- if greater than previous value, add difference to "Counter"
- else (lower than saved value, meaning it has been reinitialized), add (full) value to "Counter"
- save value as previous for next turn

Code: Select all

-- Example managing daily counter reset every day into cumulative counter
return {
	on = {devices = {'Elec'}},													-- Device containing today's value
	data={previousValue={initial=0}},											-- Permanent data to save precious value
	execute = function(domoticz, device)
		local counterDevice = domoticz.devices("Counter")						-- Load counter device
		local counter = counterDevice.counter									-- Load counter value
		local currentValue = device.sensorValue									-- Load elec value
		local delta = currentValue - domoticz.data.previousValue				-- Compute delta since last pass
		print(counter, currentValue, domoticz.data.previousValue, delta)
		domoticz.data.previousValue = currentValue								-- Save previous value for last pass
		if delta >= 0 then														-- Value incremented
			counter = counter + delta											-- Add delta
		else																	
			counter = counter + currentValue									-- Reinitialized, add value
		end
		print(counter)
		counterDevice.updateCounter(counter)									-- Update counter
	end
}
tarmacalastair
Posts: 95
Joined: Wednesday 30 October 2019 14:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Great Britain
Contact:

Re: Python Plugin: MqttMapper

Post by tarmacalastair »

Wow, thanks for your help. It's not working straight off but I'm having a play to see if I can get it working.
Many thanks
Running Domoticz and Dashticz beta on Raspberry Pi OS 32 bit - has anyone had success with 64 bit?

Desktop access from Ubuntu Linux
FlyingDomotic
Posts: 349
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Python Plugin: MqttMapper

Post by FlyingDomotic »

tarmacalastair wrote: Sunday 21 April 2024 20:33 Wow, thanks for your help. It's not working straight off but I'm having a play to see if I can get it working.
Many thanks
What's not working?
tarmacalastair
Posts: 95
Joined: Wednesday 30 October 2019 14:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Great Britain
Contact:

Re: Python Plugin: MqttMapper

Post by tarmacalastair »

Thanks. Well first of all, in the mapping file, if I leave the topic line as per your suggestion (just "glowmarkt") then nothing happens when MQTT receives a new value to the topic.
If I change the line to:

Code: Select all

	"topic": "glowmarkt/elecconsumptiontoday",
and publish a value of 208 to this topic (using mosquitto_pub) then the Domoticz log says:
Error: MQTT_Mapper: Can't find >elecconsumptiontoday< in >208<
So with some trial and error I''ve settled on this (similar to your example posted here on 26/02/24):

Code: Select all

{
        "Elec": {
                "topic": "glowmarkt/elecconsumptiontoday",
                "key": "elec",
                "type": "243", "subtype": "31", "switchtype": "0",
                "mapping": {"item": ""}
        }
}
But is this OK? I don't really understand what the "key" line means. I can see its value is used to populate the ID in the Domoticz device but does it do anything else?

Then for the counter script, I had to change the device name because the plugin adds its hardware name at the beginning of the created device (so mine is called "MQT mapper - Elec"):

Code: Select all

on = {devices = {'Elec'}},	
to

Code: Select all

on = {devices = {'MQT mapper - Elec}},
I started by creating a standard counter (RFX meter counter) but the values didn't look right (some days negative) so I then created an additional copy of the script and a second counter but this time an incremental one in case I'd misunderstood. So now I have 2 scripts and 2 counters. However, neither of the counters looks to be quite right and I'm not sure where to go next.

This is what I see if I compare the graph in Domoticz for the incremental counter for the last week (first day is 21st April):
Screenshot 2024-04-28 at 17-14-43 Domoticz.png
Screenshot 2024-04-28 at 17-14-43 Domoticz.png (50.42 KiB) Viewed 1209 times
Compared with the data for the same days, downloaded from the DCC (government central collection company):
Screenshot 2024-04-28 at 17-19-31 Smart Meter Reports.png
Screenshot 2024-04-28 at 17-19-31 Smart Meter Reports.png (17.48 KiB) Viewed 1209 times
The units for the table are kWh for each day but in Domoticz the graph says Wh. I'm very confused. Wonder if you might have any further thoughts?

Thanks
Running Domoticz and Dashticz beta on Raspberry Pi OS 32 bit - has anyone had success with 64 bit?

Desktop access from Ubuntu Linux
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 0 guests