Reporting state history using influxdb - event at HA startup

RE influxdb - makes sense. I will need to find a way to filter out the data created at startup in my grafana report. That should be possible I think…

Thanks for spotting the issue with my trigger. This setup is all new so not properly tested yet. I need this to fire all the time when the state is above 0.1. Can you help with this?

I see what you mean with the numeric state trigger now I have read about threshold.

What about this as a solution (as i think it could also help with the grafana report).

If I have an automation that resets the state of this sensor to 0 each time it is triggered. Then it would be ready for the next event and should always have a state of 0 at HA restart. Then I just filter all of the 0 state data from my report?

It seems like a clumsy and inefficient way of fixing this. What would you do?

Try this. It only triggers when a speeding event occurs. It should solve your start up issue too I think. Maybe.

template:
  - trigger:
      - platform: template
        value_template: >
          {% 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 }}
    sensor:
      - name: "Jago Speeding Event"
        unique_id: jago_speeding_event
        icon: "mdi:map-marker-radius"
        state: > 
          {% 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 }}
        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 }}

Also what were you trying to do here, ususally you only use one or the other:

 | float(0) | int(0) }}

If you want to round the result to an integer rather than truncating it use:

 {{ (speed - limit) | float | round(0) | int }}

Just usniing | int will truncate the value to an integer, not round it.

Just using | float | round(0) occasionally has epsilon errors that don’t round correctly, the extra | int filter will remove that.

You can also use a formatting function:

 {{ '%0.0f'|format((speed - limit) | float) }}
1 Like

Also the availability template means you don’t have to define default values for your float filters (except in the trigger). So you can just use |float instead of |float(0)

Legend. Thanks so much for all this input @tom_l. I will try this and see how it works.

This project is so close now. I have gps data from a phone. Calculate speed. Cleans crap data by calculating acceleration between data points. Pushes the clean data into traccar. Tracar works out speedlimit and pushes the data back into HA. Influx holds only the data where there was a ‘speeding’ event.

No worries. I’m less sure of this now

As triggered template sensors have their value restored after a restart, so you will still get the unknown → value issue after a restart causing the influx integration to log the value.

You will likely still need to filter this in Grafana.

no worries Tom. Filtering in grafana should be fine.

Do you think setting the state of this sensor to 0 at shutdown is a good way to filter?

No that won’t prevent the state going unknown after a restart. All entities go unknown after a restart until they are restored. This is because Home Assistant has no idea what happened while it was off so cannot assume any state values stayed the same.

1 Like

Would it be worth trying an automation that stopped the influxdb addon before shutdown and restarting it after HA fully starts and has loaded all the states? So using hassio.addon_stop and hassio.addon_start. No idea if this would work, but it sounds feasible.

1 Like

That is a good suggestion. Thanks. I am running HA in a container so I don’t use addons. Can I take the same approach but disable the influxdb integration?

Not sure that I can disable this integration as it is hard coded in confuration.yaml

The Spook integration has the ability to enable and disable entities.

When you re-enable the template sensor after a restart it will remain unknown until it triggers next.

Understood. But the sensor state will be 0 at startup and I can easily filter the grafana report to exclude events with state = 0

Another thanks! I’ll have a look at this. It might be a bit cleaner than filtering the report in grafana

Oh right. I thought it was always reporting 0 at start up. I see now that it would report whatever the value before restart was. You can do it like this:

template:
  - trigger:
      - platform: template
        value_template: >
          {% 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 }}
      - 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 }}
1 Like

Brilliant. I have learnt so much today.

To understand, this version has 2 triggers and they have id 0 and 1. When the shutdown trigger (id 1) is true then the state is set to 0.

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