#Make a Template Sensor that Only Updates Once a Day/Hour?

1 messages · Page 1 of 1 (latest)

lapis mesa
#

I have template sensors that measure the average energy consumption for certain devices over the last 7 days. However, it doesn't make sense for it to be updating constantly throughout the day.

I've read this documentation: https://www.home-assistant.io/integrations/template/ But it doesn't seem to be helping because I'm not sure how triggers interact with the sensor.

I was testing it with trying to get it to only update every hour. Here's the code I thought would work:

`- triggers:

  • trigger: time_pattern

    This will update every hour

    minutes: 0
    sensor:
  • name: "Bedroom AC Plug Average"
    unique_id: bedroomacplugaverage
    icon: mdi:home-lightning-bolt
    state: >-
    {% if ( states('sensor.bedroom_ac_consumption_over_7_days') | float ) <= 0 %}
    0
    {% else %}
    {{ (( states('sensor.bedroom_ac_consumption_over_7_days') | float ) / 7 ) | round(3) }}
    {% endif %}
    attributes:
    friendly_name: "Bedroom AC Plug Average"
    unit_of_measurement: kWh`

But, when I check it in states, it still seems to update when states('sensor.bedroom_ac_consumption_over_7_days') has a significant update. This equates to roughly every 70-80 seconds while Bedroom AC is running. I've tried trying to get the indentation to closer match the documentation, but file editor and visual code studio give me errors when matching it up.

When I try to google/duckduckgo search it, I just get pages with generic information about trigger sensors. When I try to find a video on YouTube, I can't seem to find a video about how to get what I want. It all seems to be videos on how to write a basic sensor or how to use triggers in automations.

I do have template: !include_dir_merge_list config/templates in configuration.yaml.

Home Assistant

Instructions on how to integrate Template Sensors into Home Assistant.

distant sparrow
#

Are you putting it in the template section? Are you reading the configuration errors?

lapis mesa
distant sparrow
#

Don’t pay attention to vscode.

#

Pay attention to check config and only check config

lapis mesa
#

Yeah, I dumped VS Code Studio and went back to only using file editor. Config tells me everything's fine. I reload the YAML, and the sensor keeps updating every 1-2 minutes.

distant sparrow
#

That’s not possible with your config above. So post what you currently have.

lapis mesa
#

`- triggers:

  • trigger: time_pattern
    minutes: "37"
    sensor:
    • name: "Bedroom AC Plug Average"
      unique_id: bedroomacplugaverage
      icon: mdi:home-lightning-bolt
      state: >-
      {% if ( states('sensor.bedroom_ac_consumption_over_7_days') | float ) <= 0 %}
      0
      {% else %}
      {{ (( states('sensor.bedroom_ac_consumption_over_7_days') | float ) / 7 ) | round(3) }}
      {% endif %}
      attributes:
      friendly_name: "Bedroom AC Plug Average"
      unit_of_measurement: kWh`
#

Just put this in to try, and I did a FULL restart of HA to test this out.

distant sparrow
#

Remove 2 spaces in front of sensor

lapis mesa
#

I'll try that now.

#

Minutes is at 40. Got an update at 4:40:10. Now I gotta wait and see if it keeps updating minute after minute.

#

(Gotta run the AC to get it to change things, oh boy.)

#

Last updated seems to be staying at 4:40:10 (good sign). The documentation showed Sensor being aligned with the text right above it, not the dash before "trigger". Maybe the documentation needs an update. Also, I wish file editor would let me remove two spaces without removing four spaces automatically and making me manually put two spaced back.

#

Could I attach multiple sensors to this trigger so I don't have to write the trigger over and over? Like this?

`- triggers:

  • trigger: time_pattern
    minutes: "37"
    sensor:
    • name: "Plug 1"
      state: >- {{ does stuff }}
      sensor:
    • name: "Plug 2"
      state: >- {{ does stuff }}`
distant sparrow
distant sparrow
lapis mesa
#

`- triggers:

  • trigger: time_pattern
    minutes: "37"
    sensor:
    • name: "Plug 1"
      state: >- {{ does stuff }}
    • name: "Plug 2"
      state: >- {{ does stuff }}`

Like this?

distant sparrow
#

Yes