Reporting state history using influxdb - event at HA startup

Hi all, I hope someone can help with this as I am so close…

I have a template sensor that is triggered using numeric_state platform of another sensor. This seems to be working fine and I can see when it is triggered using the logbook.

I am using influxdb to report the history of this sensor and this is also working.

The issue is that influxdb reports an event whenever HA starts but I am not seeing the event in the logbook so I don’t know how to stop this from happening.

Any ideas?

I’m not entirely clear what you are asking.

Report it where?

Stop what from happening?

Can you please share your template sensor config and your influxDB sensor config.

thanks @tom_l - and apologies for being vague.

Viewing the data from this sensor in influxdb or grafana. Every time I restart HA I think each sensor reports its state into influxdb creating ‘phamtom’ events.

I am looking at the the influx integration issues and I think this is an issue with influxdb not my HA config…

Below is the template sensor. This is working correcly.

- trigger:
  - platform: numeric_state
    entity_id: sensor.jago_speeding_log
    above: 0.1
  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(0) %}
        {% if speed > limit %}{{ (speed - limit) | float(0) | int(0) }}{% else %}{% endif %}      
      attributes:
        who: "Jago"
        log_state: "{{ states('sensor.jago_speeding_log') }}"
        limit_kmh: "{{ state_attr('sensor.jago_speeding_log', 'limit_kmh') | float(0) }}"
        speed_kmh: "{{ state_attr('sensor.jago_speeding_log', 'speed_kmh') | float(0) }}"
        lat: "{{ state_attr('sensor.jago_speeding_log', 'lat') | float(0) }}"
        lon: "{{ state_attr('sensor.jago_speeding_log', 'lon') | float(0) }}"
        time: "{{ state_attr('sensor.jago_speeding_log', 'time') }}"
        address: "{{ states('sensor.jago_address') }}"

And below is a table/report using grafana (connected to influxdb). The entries are mainly from me restarting HA as I have been trying to work out what is going on…

Oh ok. Pretty sure you can’t do anything about that.

The influxDB integration sends values to to the InfluxDB database whenever the state value changes. Nothing you can do about that, it’s the way the integration works.

Whenever Home Assistant restarts all entity states are set to unknown until their integration is loaded and the state value is restored. Nothing you can do about that either, it’s the way Home Assistant works.

So after a restart your sensor changes from unknown to some value and this change is reported to InluxDB.

You could probably filter out 0 values in your Grafana query.

Also be aware that this trigger:

- trigger:
  - platform: numeric_state
    entity_id: sensor.jago_speeding_log
    above: 0.1

Will only trigger when the sensor changes from below 0.1 to above 0.1. For example if you want it to trigger when changing from 10 to 20 this will not occur.

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.