custom-components / pyscript

Pyscript adds rich Python scripting to HASS
Apache License 2.0
874 stars 46 forks source link

state_trigger: trigger upon arrival of a new data packet (localtuya)? How to get the trigger status inside the triggered script? #477

Open AustrianSimon opened 1 year ago

AustrianSimon commented 1 year ago

Hello, Craig,

I can not find any documentation regarding the state_trigger and accessing the trigger status from inside the triggered routine:

Example:

@state_trigger('sensor.gncc1' or 'sensor.gncc1.last_updated') def handle_gncc1():

!/usr/bin/env python3

import sys
# I now want to find out, what actually caused the trigger, and would like to explore other options of how the trigger can be programemd
gncc1_attributes=state.getattr('gncc1') etc. do not reveal anything with respects to the trigger, trying to get the attributes of handle_gncc1 also does not bring up anything. I see quite some flags and attributes in the pycode modules however, so the calling routine has a lot of status information.

I was able to find after a long search, that indeed the last_updated information can be found by just appending it to the sensor name, however, there is no list of such properties (which may or may not solve my current issue):

I am trying to convert the smart plug datapoint "add_ele" into a power consumption after recognizing that the name "add electricity" is quite fitting and doing a test on the Tuya Cloud summing up all those data points in the device log and getting an accurate energy consumption with that. Each such "add_ele" field has to be added exactly one time, not zero times, not two or more times.

Right now the HASS does not provide for any mechanism to detect the arrival of such a data packet, only if it has a different value to the one before - which for add_ele is not possible, with a stable consumer the value will be the same repeatedly and with the current trigger mechanisms will be missed therefore (last_update and last_change are always in sync, last_update does not get set when a packet with the same value arrives - apart from the fact that updates also occur by the system etc. introducing errors).

Servus, Simon