Hi all,
I have this automation that tells me if a identified person lost weight compared to the previous day. Or it should anyway!
I have a sensor.person_weight (that is triggered by an xiaomi scale/integration) that holds just the weight recording (and a timestamp + bmi) and a sensor.person_weight_history (sensor constructed separately) that holds a history of up to 5 previous weight recordings (as well as some other items).
state_attr(‘sensor.person_weight_history’,‘history’):
[{'weight': 107.2, 'bmi': 32.72, 'time': '2023-05-09 06:53:20'}, {'weight': 107.2, 'bmi': 32.72, 'time': '2023-05-09 06:53:13'}, {'weight': 107.2, 'bmi': 32.72, 'time': '2023-05-09 06:53:11'}, {'weight': 108.2, 'bmi': 33.03, 'time': '2023-05-08 18:38:57'}, {'weight': 108.2, 'bmi': 33.03, 'time': '2023-05-08 18:38:54'}]
{{ states(‘sensor.atv_weight’) }}:
107.2
In my automation (that triggers upon when sensor.atv_weight changes) i have a template condition as follows:
{{ states('sensor.atv_weight') != 'unknown' or 'unavailable' }}
{% set w=state_attr('sensor.atv_weight_history','history') %}
{{ w if w is iterable and w|count > 0 and w[0].weight is defined }}
{{ state_attr('sensor.atv_weight','weight') < w[1].weight or
state_attr('sensor.atv_weight','weight') < w[2].weight or
state_attr('sensor.atv_weight','weight') < w[3].weight and
state_attr('sensor.atv_weight','timestamp')[8:10] > w[1].time[8:10] or
state_attr('sensor.atv_weight','timestamp')[8:10] > w[2].time[8:10] or
state_attr('sensor.atv_weight','timestamp')[8:10] > w[3].time[8:10]}}
As the xiaomi scale records up to 3 recordings, i chose to compare to check at least one extra item and start at [1] rather then [0]. The above of course checks if weight was lost and if the data is larger then yesterday.
Now when i go in developer tools, it passes as true. But when the automation runs and i look in the trace, the condition always fails and i’m not sure why. Wish there was some sort of step method to go through a live automation.
Thanks for reading.