Hourly updating template sensors

Hey there,
apparently I’m too stupid for a probably pretty simple line of code…
I am in the process of completing my energy monitoring setup, which works like a charm with Shelly Plugs, Shelly EM, InfluxDB and Grafana.

To include all loads in my household, I want to avoid installing physical meters for loads that are more or less constant eg. my networking equipment.

I set up a template sensor for that:

  - platform: template
    sensors:
      network_equipment_power:
        value_template: '{{ 26 }}'
        friendly_name: 'Stromverbrauch Netzwerkequipement'
        unit_of_measurement: 'W'

I read a lot of threads, but somehow couldn’t figure out the easiest way of updating the template sensor every hour.
Can someone give me a heads up whether to use the Sensor.time, now().hourly or something entirely different and how to implement the code in the template? Would be very much appreciated!

I’m not really sure why you want a template sensor that just updates from a manually entered constant but…

to do an hourly update you need to set up an automation that will use a time_pattern trigger set to trigger every hour.

then the action will run the homeassistant.update_entity service on the template sensor entity you created above.

1 Like

Thanks for the suggestion! My intention was to use to use this sensor to calculate the kWh like with the “real” sensors, so I can use ist side by side. If there is another way to do that, I am open to go down that route!

I tried the automation, it executes every hour:

2021-02-08 10:00:00 INFO (MainThread) [homeassistant.components.automation.template_sensor_uptade_jede_stunde] Template Sensor update jede Stunde: Running automation actions
2021-02-08 10:00:00 INFO (MainThread) [homeassistant.components.automation.template_sensor_uptade_jede_stunde] Template Sensor update jede Stunde: Executing step call service

So far so good. But somehow I can’t select any entities with the homeassistant.update_entity action. When I manually add the sensor to the automation (like shown below), it isn’t updated as well…still shows as unavailable… Any ideas?

alias: Template Sensor update jede Stunde
description: ''
trigger:
  - platform: time_pattern
    hours: '*'
condition: []
action:
  - service: homeassistant.update_entity
    data: {}
    entity_id: sensor.network_equipment_power
mode: single

grafik

If I manually reload the templates under server control it works:
grafik

Do you have a mqtt broker? You can make an automation like

alias: New Automation
description: ''
trigger:
  - platform: time_pattern
    hours: '*'
condition: []
action:
  - service: mqtt.publish
    data:
      topic: my/fixed/sensor
      payload: 26
mode: single

Then a sensor

sensor:

  - platform: mqtt
    name: "fixed"
    state_topic: "my/fixed/sensor"
    unit_of_measurement: "W"

This way you send an update to a sensor rather than poll a sensor that never updates.

1 Like

Thanks, the MQTT route seemed to have worked!

1 Like

I created the exact same template sensor as you and the same automation to update it and it’s been working fine for the last 24 hours.

The only small change I made was to use a “every hour” update (‘/1’) instead of the wild card match. That’s the recommendation I made above but you used the wildcard match instead.

Maybe that’s why yours isn’t working?

sensor:
  - platform: template
    sensors:
      network_equipment_power:
        value_template: '{{ 26 }}'
        friendly_name: 'Stromverbrauch Netzwerkequipement'
        unit_of_measurement: 'W'

automation:
  - alias: Template Sensor update jede Stunde
    description: ''
    trigger:
      - platform: time_pattern
        minutes: '/1'
    condition: []
    action:
      - service: homeassistant.update_entity
        data: {}
        entity_id: sensor.network_equipment_power
    mode: single

ex