jata
September 17, 2024, 1:04am
1
I have had some amazing help with a template sensor and I can see that the logic/trigger is working correctly. But for some reason the state is not being updated as I hoped and I can’t see why it is not working.
below is the yaml and i expected the state to be updated to [speed_kmh - limit_kmh] (should be 18 looking at the data in the image below.
Any assistance appreciated.
Template yaml:
- trigger:
- platform: template
value_template: >
{% set log_state = states('sensor.jago_speeding_log') | float(0) %}
{% set speeding = 0.1 %}
{{ log_state > speeding }}
- platform: homeassistant
event: 'shutdown'
sensor:
- name: "Jago Speeding Event"
unique_id: jago_speeding_event
icon: "mdi:map-marker-radius"
state: >
{% if trigger.id == 0 %}
{% set speed = state_attr('sensor.jago_speeding_log', 'speed_kmh') | float %}
{% set limit = state_attr('sensor.jago_speeding_log', 'limit_kmh') | float %}
{{ speed - limit }}
{% else %}
0
{% endif %}
attributes:
who: "Jago"
log_state: "{{ states('sensor.jago_speeding_log') }}"
limit_kmh: "{{ state_attr('sensor.jago_speeding_log', 'limit_kmh') }}"
speed_kmh: "{{ state_attr('sensor.jago_speeding_log', 'speed_kmh') }}"
trigger_id: "{{ trigger.id }}"
lat: "{{ state_attr('sensor.jago_speeding_log', 'lat') }}"
lon: "{{ state_attr('sensor.jago_speeding_log', 'lon') }}"
time: "{{ state_attr('sensor.jago_speeding_log', 'time') }}"
address: "{{ states('sensor.jago_address') }}"
availability: >
{{ state_attr('sensor.jago_speeding_log', 'speed_kmh')|is_number and
state_attr('sensor.jago_speeding_log', 'limit_kmh')|is_number }}
Trigger confirmation
Sir_Goodenough
((SG) WhatAreWeFixing.Today)
September 17, 2024, 1:24am
2
Because limited templates I believe. There is no set command there.
You are working with static and know values, so I would try setting a template_variable section, and create a variable that is the target you are looking for an trigger off of that.
Trigger_Variables - variables that are available when attaching a trigger .
Just like states, in a trigger-based template sensor attributes are only updated following triggers… and you don’t have a reset mechanism in the templates of the attributes.
jata
September 17, 2024, 1:38am
4
I see. Thanks.
I have a dependent triggered sensor setup in a similar way and it is working. Why does this one work correctly? The only significant difference is this one does not use an availability template.
- trigger:
- platform: template
value_template: >
{% set speed = states('sensor.jago_speed') | float(0) %}
{% set driving_speed = 22 %}
{{ speed > driving_speed }}
sensor:
- name: "Jago Speeding Log"
unique_id: jago_speeding_log
icon: "mdi:speedometer"
state: >
{% set speed = states('sensor.jago_speed') | float(0) %}
{% set limit = state_attr('device_tracker.jago', 'speedLimit') | float(0) %}
{% if speed == 0 or limit == 0 %}
{% set speeding_percent = 0 %}
{% else %}
{% set speeding_percent = ((speed - limit) / limit) | round(2) %}
{% endif %}
{{ speeding_percent }}
attributes:
speed_kmh: "{{ (states('sensor.jago_speed') | float(0) * 1.852 ) | round(1) }}"
limit_kmh: "{{ (state_attr('device_tracker.jago', 'speedLimit') | float(0) * 1.852 ) | round(1) }}"
lat: "{{ state_attr('device_tracker.jago', 'latitude') }}"
lon: "{{ state_attr('device_tracker.jago', 'longitude') }}"
time: "{{ now().timestamp() | timestamp_custom('%a %d/%m %I:%M %p') }}"
jata
September 17, 2024, 1:46am
5
Right. This could explain it i think…
So the triggering sensor (speeding log) has not updated its attributes so the data is not yet available for the receiving sensor (speeding event).
I’m a learner so is this a correct understanding?
jata
September 17, 2024, 6:04am
6
For anyone who has a similar issue, what I found is that the problem was that I was using the trigger.id in the state template and this was preventing the state being updated.
So I have now simplified the entire approach and I hold the trigger.id as an attribute and use this to filter the data in a report.
Working yaml below…
# Triggers when 10% (0.1) or above limit
- trigger:
- platform: template
value_template: >
{% set speed = states('sensor.jago_speed') | float(0) %}
{% set limit = state_attr('device_tracker.jago', 'speedLimit') | float(0) %}
{% if speed == 0 or limit == 0 %}
{% set speeding_percent = 0 %}
{% else %}
{% set speeding_percent = ((speed - limit) / limit) | float %}
{% endif %}
{{ speeding_percent > 0.1 }}
- platform: homeassistant
event: 'shutdown'
sensor:
- name: "Jago Speeding Event"
unique_id: jago_speeding_event
icon: "mdi:map-marker-radius"
state: >
{% set speed = states('sensor.jago_speed') | float %}
{% set limit = state_attr('device_tracker.jago', 'speedLimit') | float %}
{{ ((speed - limit) * 1.852 ) | int }}
attributes:
who: "Jago"
trigger_id: "{{ trigger.id }}"
speed_kmh: "{{ (states('sensor.jago_speed') | float * 1.852 ) | int }}"
limit_kmh: "{{ (state_attr('device_tracker.jago', 'speedLimit') | float * 1.852 ) | int }}"
lat: "{{ state_attr('device_tracker.jago', 'latitude') }}"
lon: "{{ state_attr('device_tracker.jago', 'longitude') }}"
time: "{{ now().timestamp() | timestamp_custom('%a %d/%m %I:%M %p') }}"
address: "{{ states('sensor.jago_address') }}"
availability: >
{{ states('sensor.jago_speed')|is_number and
state_attr('device_tracker.jago', 'speedLimit')|is_number }}