Page 1 of 1

Spltting temp humidity pressure sensor wrong decimal value

Posted: Wednesday 11 December 2019 15:20
by yfands
Hi all.

Via MQTT I receive in Domoticz the following string:
2019-12-11 14:50:33.977 MQTT: Topic: domoticz/in, Message: {"idx":783,"nvalue":0,"svalue":"19.5;57.9;1;1004.5;5","Battery":100,"RSSI":7}

Iam interested in the svalue which must be
temp 19.5
humidity 57.9
humidity text value 1
pressure 1004.5
pressure text value 5

notice that ALL devices have decimal values and that is what I want Domoticz to display via the Custom Sensors option.

Whatever I try to fill in after the match function nothing works to get my pressure to show with the decimal part, Its getting rounded
the moment it gets assigned to a variable.

part of my code:

Spoiler: show
local Huiskamer_THB_idx = 783
local Huiskamer_Pressure_idx = 814

commandArray = {}

if devicechanged['Huiskamer_THB_idx'] then

-- Split
Huiskamer_Temp, Huiskamer_Humidity, Huiskamer_Humidity_Text,Huiskamer_Pressure,Huiskamer_Forecast_Text = otherdevices_svalues['Huiskamer_THB_idx']:match("(%d+%.*%d*);(%d+%.*%d*);([^;]+);(%d+%.*%d*);([^;]+)")

--Huiskamer_Pressure = tonumber(Huiskamer_Pressure)
commandArray[1]={['UpdateDevice'] = Tasmota_Huiskamer_Pressure_idx.. '|0|' .. (Tasmota_Huiskamer_Pressure)}

print(Tasmota_Huiskamer_Temp)
print(Tasmota_Huiskamer_Humidity)
print(Tasmota_Huiskamer_Humidity_Text)
print(Tasmota_Huiskamer_Pressure)
print(Tasmota_Huiskamer_Forecast_Text)

end

return commandArray
the print part part shows this

2019-12-11 14:50:34.249 Status: LUA: 19.50 <-- even added an extra 0
2019-12-11 14:50:34.249 Status: LUA: 57.9
2019-12-11 14:50:34.249 Status: LUA: 1
2019-12-11 14:50:34.249 Status: LUA: 1004 <-- no decimal
2019-12-11 14:50:34.249 Status: LUA: 5

I studied several split LUA examples and the split example in DZvents but non of them work. In DZvents even the two last variables are not shown :-(

Who has any ideas?
Thanks in advance
Frank

Re: Spltting temp humidity pressure sensor wrong decimal value

Posted: Wednesday 11 December 2019 16:14
by boum
You may have to post a bit more of your code. The variables you assigned the matched strings are not the same as the printed ones.

Also, I just ran this in a dzVents scripts:

Code: Select all

		local svalue = "19.5;57.9;1;1004.5;5"
		local t, h, tt, p, f = svalue:match("(%d+%.*%d*);(%d+%.*%d*);([^;]+);(%d+%.*%d*);([^;]+)")
		print( tonumber(t) )
		print( tonumber(h) )
		print( tt )
		print( tonumber(p) )
		print( f )
And I got the expected results:
Spoiler: show
2019-12-11 16:12:59.645 Status: dzVents: 19.5
2019-12-11 16:12:59.646 Status: dzVents: 57.9
2019-12-11 16:12:59.646 Status: dzVents: 1
2019-12-11 16:12:59.646 Status: dzVents: 1004.5
2019-12-11 16:12:59.646 Status: dzVents: 5

Re: Spltting temp humidity pressure sensor wrong decimal value

Posted: Wednesday 11 December 2019 16:26
by waaren
yfands wrote: Wednesday 11 December 2019 15:20 Via MQTT I receive in Domoticz the following string:
2019-12-11 14:50:33.977 MQTT: Topic: domoticz/in, Message: {"idx":783,"nvalue":0,"svalue":"19.5;57.9;1;1004.5;5","Battery":100,"RSSI":7}
notice that ALL devices have decimal values and that is what I want Domoticz to display via the Custom Sensors option.
Whatever I try to fill in after the match function nothing works to get my pressure to show with the decimal part, Its getting rounded
the moment it gets assigned to a variable.
The issue is not in Lua, dzVents or your match statement. The issue is that domoticz interprets the MQTT svalue in another way then you expect.
I defined a temp+hum+baro device in my test system (id = 189). Then I sent an MQTT

Code: Select all

 mosquitto_pub -h  pi-6  -m '{"idx":189,"nvalue":0,"svalue":"19.5;57.9;1;1004.5;5","Battery":100,"RSSI":7}' -t domoticz/in
see what ended up in the database...

Code: Select all

sqlite3 domoticz.db
SQLite version 3.27.2 2019-02-25 16:06:06
sqlite> .he on
sqlite> select id,svalue from devicestatus where id=189 ;
ID|sValue
189|19.50;57.9;1;1004;5
A workaround could be to store the svalue as string in a domoticz uservariable and interpret the values from the uservar and store them at the right places in the target device(s).
send to domoticz MQTT

Code: Select all

mosquitto_pub -h  pi-6  -m '{"command": "setuservariable", "idx": 11, "value": "19.5;57.9;1;1004.5;5" }' -t domoticz/in
dzVents script

Code: Select all

return 
{
    on = 
    {
        variables = 
        {
            'testMQTT' -- name of (string) variable with index 11
        }
    },
    
    execute = function(dz, item)
        dz.utils.dumpTable(dz.utils.stringSplit(item.value,';'))
    end
}
result

Code: Select all

2019-12-11 16:21:59.367 Status: dzVents: Info: ------ Start internal script: dz stringsplit uservar: Variable: "testMQTT" Index: 11
2019-12-11 16:21:59.368 Status: dzVents: > 1: 19.5
2019-12-11 16:21:59.368 Status: dzVents: > 2: 57.9
2019-12-11 16:21:59.369 Status: dzVents: > 3: 1
2019-12-11 16:21:59.369 Status: dzVents: > 4: 1004.5
2019-12-11 16:21:59.369 Status: dzVents: > 5: 5
2019-12-11 16:21:59.370 Status: dzVents: Info: ------ Finished dz stringsplit uservar

Re: Spltting temp humidity pressure sensor wrong decimal value

Posted: Wednesday 11 December 2019 22:37
by yfands
Hi Guys

@Boum, yes the 'match' with a 'normal' string works like a charm, should have mentioned that, sorry.
Thats why I couldn't understand why it wouldn't work with the MQTT string.

@Waaren, thanks for the clarification, there is a lot going on regarding Domoticz handling decimal places,
I for one say let the user decide, its that simple. With each sensor properties a selection to make how many decimals, everybody happy.
I use the MAX44009 an Illumination sensor, Lux range 0.045 Lux to 188,000 Lux, Default Domoticz use this sensor is overkill. But as a custom sensor you can use the fraction. Between 0.045 or a plain 1 Lux falling darkness value it can take up to 20 minutes.

Gonne try to implement the user variable solution, thank you.

Regards
Frank