Page 1 of 1

How to separate values in Python Event where the n_value or s_value returns more than 1?

Posted: Wednesday 12 April 2023 7:38
by mvdheide01
Hi,
I am struggling with the following task.
My electric boiler switches on 1 hour after sunrise so it can benefit the most from my solar panels.
I would like to switch on the charging of my electric car, after the boiler is done.
There is a smart switch on my boiler, which measures the power.
The challenge is, that this switch gives me 2 values
0.000;14030760.000 when not in use and for example
2523.100;14030760.000 when it is heating.
so the value for current usage is before the ; and the total usage behind the ;
What is the correct way to get the value from current usage so I can make a if statement that when current usage is below 1500, the charging of the car can start?
Cuurent try:

import DomoticzEvents as DE

if DE.changed_device_name == "Boiler kWh Meter":
DE.Log("--Python: Changed: " + DE.changed_device.Describe() )
DE.Log("--Python: device: " + str(DE.changed_device.id) + " " + str(DE.changed_device.name) )
DE.Log("--Python: device: " + str(DE.changed_device.id) + " " + str(DE.changed_device.name) + " n_value : " + str(DE.changed_device.n_value) )
DE.Log("--Python: device: " + str(DE.changed_device.id) + " " + str(DE.changed_device.name) + " n_value_string : " + str(DE.changed_device.n_value_string) )
# n_value_string gives --Python: device: 12 Boiler kWh Meter n_value_string : 0.000;14028750.000
# which seems to be current usage and total usage. How to get only the current usage?
DE.Log("--Python: device: " + str(DE.changed_device.id) + " " + str(DE.changed_device.name) + " s_value : " + str(DE.changed_device.s_value) )

Re: How to separate values in Python Event where the n_value or s_value returns more than 1?

Posted: Wednesday 12 April 2023 9:00
by willemd
I think you can split the values with the utils.stringSplit() function.
https://www.domoticz.com/wiki/DzVents:_ ... _scripting

Re: How to separate values in Python Event where the n_value or s_value returns more than 1?

Posted: Wednesday 12 April 2023 9:40
by waltervl
Just a thought: If you are already struggling with Python it is perhaps better for you to move to DzVents for event scripts. Lots of examples, much better integration, no memory leaks, better documentation, better support etc. But just my thoughts. Please do not feel offended.

Re: How to separate values in Python Event where the n_value or s_value returns more than 1?

Posted: Wednesday 12 April 2023 10:48
by mvdheide01
The idea to stick to Python is that at work it is also used and it is already hard enough to learn. But ofcourse in a different way so hard to combine.
I use blockly for some other scenes but was not able to use the value of the current usage, probably because it gets 2 values.

Re: How to separate values in Python Event where the n_value or s_value returns more than 1?

Posted: Wednesday 12 April 2023 11:15
by willemd
Also python has a split() method.

Re: How to separate values in Python Event where the n_value or s_value returns more than 1?

Posted: Thursday 13 April 2023 16:09
by mvdheide01
Thanks!, that helped me a lot.
It works fine (for now) so here is what I made of it:
import DomoticzEvents as DE

if DE.changed_device_name == "Boiler kWh Meter":
DE.Log("--Python: Changed: " + DE.changed_device.Describe() )
DE.Log("--Python: device: " + str(DE.changed_device.id) + " " + str(DE.changed_device.name) )
DE.Log("--Python: device: " + str(DE.changed_device.id) + " " + str(DE.changed_device.name) + " n_value : " + str(DE.changed_device.n_value) )
DE.Log("--Python: device: " + str(DE.changed_device.id) + " " + str(DE.changed_device.name) + " n_value_string : " + str(DE.changed_device.n_value_string) )
# n_value_string gives --Python: device: 12 Boiler kWh Meter n_value_string : 0.000;14028750.000
DE.Log("--Python: device: " + str(DE.changed_device.id) + " " + str(DE.changed_device.name) + " s_value : " + str(DE.changed_device.s_value) )
# which seems to be current usage and total usage. How to get only the current usage?
currentusage,totalusage = DE.changed_device.n_value_string.split(';')
DE.Log("--Python: device: " + str(DE.changed_device.id) + " " + str(DE.changed_device.name) + " n_value_string.currentusage : " + str(currentusage) )
DE.Log("--Python: device: " + str(DE.changed_device.id) + " " + str(DE.changed_device.name) + " n_value_string.totalusage : " + str(totalusage) )

DE.Log("--Python: currentusage of device: " + (currentusage))
DE.Log("--Python: totalusage of device: " + (totalusage))
if float(currentusage) > 1500:
DE.Log("--Python: device: " + str(DE.changed_device.id) + " " + str(DE.changed_device.name) + " n_value_string.currentusage > 1500 : " + str(currentusage) )
if DE.Devices["Boiler verwarmt"].n_value_string == "Off":
DE.Log("-- Python Zet schakelaar Boiler verwarmt: AAN")
DE.Command("Boiler verwarmt", "On")
else:
DE.Log("--Python: device: " + str(DE.changed_device.id) + " " + str(DE.changed_device.name) + " n_value_string.currentusage 0-1500 : " + str(currentusage) )
if DE.Devices["Boiler verwarmt"].n_value_string == "On":
DE.Log("-- Python Zet schakelaar Boiler verwarmt: UIT")
DE.Command("Boiler verwarmt", "Off")