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

Python and python framework

Moderator: leecollings

Post Reply
mvdheide01
Posts: 19
Joined: Monday 12 November 2018 19:05
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Belgium
Contact:

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

Post 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) )
Raspberry Pi, RFXCOM - RFXtrx433 USB 433.92MHz Transceiver, OpenZWave USB
willemd
Posts: 649
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

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

Post by willemd »

I think you can split the values with the utils.stringSplit() function.
https://www.domoticz.com/wiki/DzVents:_ ... _scripting
User avatar
waltervl
Posts: 5853
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

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

Post 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.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
mvdheide01
Posts: 19
Joined: Monday 12 November 2018 19:05
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Belgium
Contact:

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

Post 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.
Raspberry Pi, RFXCOM - RFXtrx433 USB 433.92MHz Transceiver, OpenZWave USB
willemd
Posts: 649
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

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

Post by willemd »

Also python has a split() method.
mvdheide01
Posts: 19
Joined: Monday 12 November 2018 19:05
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Belgium
Contact:

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

Post 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")
Raspberry Pi, RFXCOM - RFXtrx433 USB 433.92MHz Transceiver, OpenZWave USB
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest