Update template sensor at regular intervals

I have a template sensor which is essentially used just to turn a state attribute (in this case humidity) into an entity of its own. I would like to track this and use the trend platform to detect sudden spikes in humidity. However, in order to do this I need readings from the template sensor at a regular interval (eg. every 120 seconds). Any ideas on how I can do this? Perhaps using now() ?
This is my current template sensor:

template: 
  - sensor: 
     - name: "Bathroom humidity"
       unit_of_measurement: "%"     
       state: "{{ state_attr('sensor.wiser_roomstat_bathroom', 'humidity') }}"

I am a templates novice so any help would be greatly appreciated.

You could just use a trigger based template sensor, as shown in the example.

The trend platform does not require updates every 120s, it only requires

at least two updates of the underlying sensor to establish a trend

If you want to base your trend off multiple samples you need to figure out the actual refresh rate/scan interval of “wiser_roomstat” sensor. Then use that information along with some experimentation to figure out an appropriate maximum number of samples to set to capture the “spikes”.

It seem to me that manufacturing samples would have the opposite effect than what you are looking for because it would flatten the slope of the trend line.

1 Like

Shortly after posting, I realised this. Thanks for confirming this though.

Having played around with it for a while, the problem is thus.
The “wiser_roomstat” itself cannot be used as humidity is only listed as an attribute and therefore I have a template sensor as above. The problem is that the template sensor only updates when the humidity changes leading to large gaps between updates. This results in false positives as quick small changes in humidity can achieve the required gradient even with just a 1% change due to lack of samples. Not sure how to mitigate this without manufacturing a regualar refresh rate…

You can set the the trend sensor to monitor an attribute of a sensor… you do not need a separate template sensor.

I’m not 100% sure, but I think that might create sample points on the updates of the original sensor.

binary_sensor:
  - platform: trend
    sensors:
      bathroom_humidity_trend:
        entity_id: sensor.wiser_roomstat_bathroom
        attribute: humidity
        max_samples: ?
        min_gradient: ?

Other options to look at:

1 Like

Thanks. I think I have achieved it with the template sensor with the following:

- sensor: 
     - name: "bathroom humidity"
       unit_of_measurement: "%"     
       state: "{{ state_attr('sensor.wiser_roomstat_bathroom', 'humidity') }}"
       attributes:
         attribute: "{{ now().minute }}" 

But thanks for letting me know that you can use the attribute of a sensor for trends. For some reason I missed that so if the above doesn’t work out, I shall try that! Thanks

Hi @tombadog !

I am looking for a solution to a similar problem. The difference is that I would like to subtract the value of several sensors from another sensor and present it as a template sensor. Specifically, I have the main power meter, a sub-meter for a heat pump and then several smart plugs with power measurement. I would like to see something like total power and energy use and total - heat pump etc. I created a template sensor but the values do not add up. I think that it is because all of the sensors report at different intervals. I would like to see the energy use - here I could probably gather the values with your method once a minute or something like that. Then I would like to have the power meters with momentary values refreshed for example every 10 seconds but to have it so that all are refreshed at the same time.
Would you have any suggestions? Thank you!

But how to make sensor that update every 15 minutes? i have a xiaomi thermometer with pvvx but i dont need raport every minute. Need a state every 15 minutes or 30 for my automation . how can i do that?

Hi @tombadog
Does this still work for you? Does this sensor only update every minute and therefore reduces the database impact this sensor has? Thanks.

I created the template sensor for Power with attribute: “{{ now().minute }}” . The last-updated entities showed it updates every 1 minute but when I clicked details info it showed last refresh was 1 hour ago and the data what not updated.

Has anyone been able to figure out how to control/set the scan interval for a given sensor?