Python Plugin: MqttMapper

Python and python framework

Moderator: leecollings

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

Re: Python Plugin: MqttMapper

Post by FlyingDomotic »

So, if I correctly understood, value loaded into device "MQTT mapper - T_Outside" is correct. right?

Is there another help we may provide to you?
hjzwiers
Posts: 242
Joined: Friday 12 January 2018 8:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MqttMapper

Post by hjzwiers »

Yes, it is correct!
I only wonder how to convert this value to a valid temperature, including if the value is different as I had observed earlier.
FlyingDomotic
Posts: 356
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Python Plugin: MqttMapper

Post by FlyingDomotic »

First of all, you have to check that virtual device plug-in is loaded into Domoticz:
  • goto "Setup/Hardware" tab
  • check if a line with type "Dummy (Does nothing, use for virtual switches only)" exists
  • if not, create one by going into lower part of screen, setting "name" to "Virtual devices", choosing "type" = "Dummy (Does nothing, use for virtual switches only)", and clicking on "Add"
Then click on "Create virtual sensors" just after ""Dummy (Does nothing, use for virtual switches only)" text

In the popup windows, give a device name (like "Outside temperature") and set "Sensor type" to "Temperature", then click on "Ok"

Congrats, you've just created a brand new virtual temperature sensor!

Now, goto "Setup/More options/Events", click on "+/LUA/device".

In the script windows, you'll see something like:

Code: Select all

--
-- Domoticz passes information to scripts through a number of global tables
--
-- device changed contains state and svalues for the device that changed.
--   devicechanged['yourdevicename'] = state 
--   devicechanged['svalues'] = svalues string 
--
-- otherdevices, otherdevices_lastupdate and otherdevices_svalues are arrays for all devices: 
--   otherdevices['yourotherdevicename'] = "On"
--   otherdevices_lastupdate['yourotherdevicename'] = "2015-12-27 14:26:40"
--   otherdevices_svalues['yourotherthermometer'] = string of svalues
--
-- uservariables and uservariables_lastupdate are arrays for all user variables: 
--   uservariables['yourvariablename'] = 'Test Value'
--   uservariables_lastupdate['yourvariablename'] = '2015-12-27 11:19:22'
--
-- other useful details are contained in the timeofday table
--   timeofday['Nighttime'] = true or false
--   timeofday['SunriseInMinutes'] = number
--   timeofday['Daytime'] = true or false
--   timeofday['SunsetInMinutes'] = number
--   globalvariables['Security'] = 'Disarmed', 'Armed Home' or 'Armed Away'
--
-- To see examples of commands see: http://www.domoticz.com/wiki/LUA_commands#General
-- To get a list of available values see: http://www.domoticz.com/wiki/LUA_commands#Function_to_dump_all_variables_supplied_to_the_script
--
-- Based on your logic, fill the commandArray with device commands. Device name is case sensitive. 
--
commandArray = {}

-- loop through all the changed devices
for deviceName,deviceValue in pairs(devicechanged) do
    print ("Device based event fired on '"..deviceName.."', value '"..tostring(deviceValue).."'");
--    if (deviceName=='myDevice') then
--        if deviceValue == "On" then
--            print("Device is On")
--        elseif deviceValue == "Off" then
--            commandArray['a device name'] = "On"
--            commandArray['another device name'] = "Off AFTER 10"
--            commandArray['Scene:MyScene'] = "Off"
--            commandArray['Group:My Group'] = "Off AFTER 30"
--        end
--    end
end

return commandArray
Select everything and replace text by:

Code: Select all

commandArray = {}

for deviceName,deviceValue in pairs(devicechanged) do
    if (deviceName=='MQTT mapper - T_Outside') then
        temperature = string.match(deviceValue, '[0-9%.]+')
        print("Outside temperature "..temperature)
        commandArray[#commandArray + 1] = {['UpdateDevice']= otherdevices_idx["Outside temperature"]..'||'..temperature}
    end
end

return commandArray
You may change script name from "Script #1" to something like "Outside temperature".

To save the script, click on "Save".

You can then test that any change made in "MQTT mapper - T_Outside" is properly extracted and reflected into "Outside temperature".

FYI, string.match will return every "0" to "9" and "." characters from "MQTT mapper - T_Outside" data, and commandArray syntax will load this extracted data into "Outside temperature" device.

Don't hesitate to ask questions, should you need more details.
hjzwiers
Posts: 242
Joined: Friday 12 January 2018 8:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Plugin: MqttMapper

Post by hjzwiers »

I have a lot of experience with domoticz by now, but this was a perfect guideline.

Built a script for the Room temperature and Outside temperature within 15 minutes.

Right now its in the version without the parathesis, but when it changes I'll check if it still works.

Thanks a lot for your help!!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest