One last thing…
Why is it that numeric state triggers work using a threshold? It would be great if you could configure it to use absolute value or threshold.
One last thing…
Why is it that numeric state triggers work using a threshold? It would be great if you could configure it to use absolute value or threshold.
Just a quick update on this now I have it all setup correctly (i think).
I am using 2 triggered entities. The first records speed but only when speed is over 40kmh (as i’m only interested in driving speed). The second is triggered by the first but only when the speed is 10% over the road speed limit. It is also triggered by HA shutdown.
I am expecting the second one to record an event whenever HA is restarted but I dont think this is happening as there are no logbook entries when I restart HA.
Anything obviously wrong?
speeding log (when speed > 40kmh):
# sensor in knots so 22 = 40kmh
- 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') }}"
Speeding event (when speed is > 10% higher than speed limit)
# Triggers when 10% (0.1) or above
- 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(0) %}
{% set limit = state_attr('sensor.jago_speeding_log', 'limit_kmh') | float(1) %}
{{ 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') }}"
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 }}
I wanted to update this thread in case it helps others with a similar challenge.
Turns out that using trigger.id in the state template was causing the issue. I don’t fully understand why but I think it is because the trigger.id is not set until after the state template is complete.
So what I did is to remove the if statement from the state template and added the trigger.id as a attribute. In grafana, I filter the report using the trigger.id to hide unwanted data created in influxdb/grafana when HA starts.
Here is the (finally) working results. Chart shows speedlimit and speed. Table embedded from grafana shows a list of speeding events (speed > 10% above limit).
Massive thanks to @tom_l and others for your help.
yaml for the speeding sensor
- trigger:
- platform: template
value_template: >
{% set speed = states('sensor.magda_speed') | float(0) %}
{% set limit = state_attr('device_tracker.magda', '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: "Magda Speeding Event"
unique_id: magda_speeding_event
icon: "mdi:map-marker-radius"
state: >
{% set speed = states('sensor.magda_speed') | float %}
{% set limit = state_attr('device_tracker.magda', 'speedLimit') | float %}
{{ ((speed - limit) * 1.852 ) | int }}
attributes:
who: "Magda"
trigger_id: "{{ trigger.id }}"
speed_kmh: "{{ (states('sensor.magda_speed') | float * 1.852 ) | int }}"
limit_kmh: "{{ (state_attr('device_tracker.magda', 'speedLimit') | float * 1.852 ) | int }}"
lat: "{{ state_attr('device_tracker.magda', 'latitude') }}"
lon: "{{ state_attr('device_tracker.magda', 'longitude') }}"
time: "{{ now().timestamp() | timestamp_custom('%a %d/%m %I:%M %p') }}"
address: "{{ states('sensor.magda_address') }}"
availability: >
{{ states('sensor.magda_speed')|is_number and
state_attr('device_tracker.magda', 'speedLimit')|is_number }}
If you want it to record only at start up then you want the second trigger (trigger.id == 1
), it starts counting at 0. So id 0 = 1st trigger, id 1 = 2nd trigger.
# Triggers when 10% (0.1) or above
- 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 == 1 %}
{% set speed = state_attr('sensor.jago_speeding_log', 'speed_kmh') | float(0) %}
{% set limit = state_attr('sensor.jago_speeding_log', 'limit_kmh') | float(1) %}
{{ 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') }}"
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 }}
Thanks Tom. The issue had something to do with using trigger.id in the state template.
I solved the problem in any case so thanks again for your help. My HA templating skills have gone from a 1 to a 2-3 haha