Time based trigger only triggering once

Reaching out to the community since I cannot figure this one out.

I have a trigger that is to update two sensors at the end of the hour, to get a hourly state to be able to visualize graphs in Grafana.

The trigger works the first time and sub-sequent hours for ‘electrical_yield_rate_summary_hour’.
However, the trigger only works the first time for ‘electrical_distribution_consumption_rate_summary_sek_hour’, but not for sub-sequent hours.

Both of the sensors has values.

The only difference is that ‘sensor.electrical_yield_rate_summary’ is update hourly, where-as ‘sensor.electrical_distribution_consumption_rate_summary_sek’ is only updated at reboot or when manually set.

Have I missed something here, or is this a bug/feature?

  - trigger:
      # Trigger at the near end of every hour, since we want to keep hourly states for energy costs.
      - platform: time_pattern
        hours: "/1"
        minutes: "59"
        seconds: "0"
    sensor:
      # We set the hourly state for: electrical_yield_rate_summary
      - name: electrical_yield_rate_summary_hour
        unit_of_measurement: 'SEK/kWh'
        state_class: measurement
        state: >
          {% set snapshot = states('sensor.electrical_yield_rate_summary') %}
          {% if (snapshot == 'unknown' or snapshot == 'unavailable') %}
          {%   set result = 0 %}
          {% else %}
          {%   set result = float(snapshot)/100  %}
          {% endif %}
          {{ result | round(2) }}
      # We set the hourly state for: electrical_distribution_consumption_rate_summary_sek
      - name: electrical_distribution_consumption_rate_summary_sek_hour
        unit_of_measurement: 'SEK/kWh'
        state_class: measurement
        state: >
          {% set snapshot = states('sensor.electrical_distribution_consumption_rate_summary_sek') %}
          {% if (snapshot == 'unknown' or snapshot == 'unavailable') %}
          {%   set result = 0 %}
          {% else %}
          {%   set result = float(snapshot) %}
          {% endif %}
          {{ result }}

Note also that there is no difference if I put the sensors in different order after the trigger.

Seems you can just use the ‘minutes’ part, I think yours is clashing

  trigger:
    - platform: time_pattern
      # Matches every hour at 5 minutes past whole
      minutes: 5

Automation Trigger - Home Assistant (home-assistant.io)

You don’t actually need to do any of that.

In Grafana just graph sensor.electrical_yield_rate_summary and sensor.electrical_distribution_consumption_rate_summary_sek, group them by hour and use the selector “last”.

Hmm. No I do not believe so.
I have another trigger, with similar time pattern that works flawlessly for all sensors to be updated by the trigger.

As further info, sensor.electrical_distribution_consumption_rate_summary_sek_hour’ is updated once at 11 mars 2022 08:59:00 (after reboot and first run at 8.59).
The other sensor, is updated each hour, lastly now on 11 mars 2022 16:59:00.

So, one sensor is updated continously each hour, where-as the other sensor, triggered by the same trigger, is only triggered once.

Hi Tom,
My understanding of Grafana is that the last state must be in the same time-range.
For instance if I have a ‘today’ range, and the state event is from yesterday, it will not be presented in Grafana.
This is why I wanted an hourly sensor, to ‘stamp’ each hour with the correct value.

I put HA in debug logging and sure enough the above problem is represented in the logs.

‘electrical_yield_rate_summary_hour’ triggers correctly each hour:

2022-03-11 18:59:00 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.electrical_yield_rate_summary_hour, old_state=<state sensor.electrical_yield_rate_summary_hour=unknown; state_class=measurement, unit_of_measurement=SEK/kWh, friendly_name=electrical_yield_rate_summary_hour @ 2022-03-11T17:57:00.452136+01:00>, new_state=<state sensor.electrical_yield_rate_summary_hour=2.53; state_class=measurement, unit_of_measurement=SEK/kWh, friendly_name=electrical_yield_rate_summary_hour @ 2022-03-11T18:59:00.004906+01:00>>
2022-03-11 19:59:00 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.electrical_yield_rate_summary_hour, old_state=<state sensor.electrical_yield_rate_summary_hour=2.53; state_class=measurement, unit_of_measurement=SEK/kWh, friendly_name=electrical_yield_rate_summary_hour @ 2022-03-11T18:59:00.004906+01:00>, new_state=<state sensor.electrical_yield_rate_summary_hour=2.26; state_class=measurement, unit_of_measurement=SEK/kWh, friendly_name=electrical_yield_rate_summary_hour @ 2022-03-11T19:59:00.003917+01:00>>

Where-as ‘electrical_distribution_consumption_rate_summary_sek_hour’ only triggers one time (after reboot):

2022-03-11 18:59:00 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.electrical_distribution_consumption_rate_summary_sek_hour, old_state=<state sensor.electrical_distribution_consumption_rate_summary_sek_hour=unknown; state_class=measurement, unit_of_measurement=SEK/kWh, friendly_name=electrical_distribution_consumption_rate_summary_sek_hour @ 2022-03-11T17:57:00.452904+01:00>, new_state=<state sensor.electrical_distribution_consumption_rate_summary_sek_hour=1.56; state_class=measurement, unit_of_measurement=SEK/kWh, friendly_name=electrical_distribution_consumption_rate_summary_sek_hour @ 2022-03-11T18:59:00.007416+01:00>>

Is this per design?
Should not the trigger, set the sensor, even though the state is not updated in near history?

I have done some more digging, and I more and more believe that this is per design in the state/event engine.

I triggered the update_entity service with:

service: homeassistant.update_entity
data: {}
target:
  entity_id: sensor.electrical_distribution_consumption_rate_summary_sek_hour

and I got the following error in the log

  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 146, in _async_update_data
    raise NotImplementedError("Update method not implemented")
NotImplementedError: Update method not implemented

So,we cannot trigger update_entity for template sensors.

When searching more on this community, I found the following: Add force update, which made me try the following code:

  - trigger:
      # Trigger at the near end of every hour, since we want to keep hourly states for energy costs.
      - platform: time_pattern
        hours: "/1"
        minutes: "59"
        seconds: "0"
      # We set the hourly state for: electrical_distribution_consumption_rate_summary_sek
      - name: electrical_distribution_consumption_rate_summary_sek_hour
        unit_of_measurement: 'SEK/kWh'
        state_class: measurement
        state: >
          {% set snapshot = states('sensor.electrical_distribution_consumption_rate_summary_sek') %}
          {% if (snapshot == 'unknown' or snapshot == 'unavailable') %}
          {%   set result = 0 %}
          {% else %}
          {%   set result = float(snapshot) %}
          {% endif %}
          {{ result | round(2) }}
        attributes:
          updated: >
            {{ now() }}

I added the function now() for attribute update for the entity, and now I get hourly trigger for the entity.

Why does this work, well, the superb HA developers have made it so, that if sensor include the function now(), then it is updated. See pull request here.
If I only added now() in the state-code it did not update each hour.

So, now the sensors get properly triggered each hour, even if the entity has the same value (and hence shall not update).

This is clearly a tweak from my end, but now I get hourly state triggers to Grafana, and can follow my tariff sensors properly.

I can now also close the issue I created.

1 Like