Hi guys, I’ve been trying all day to create a sensor that tells me how many days an entity has been in the same state. Specifically, I want it for my rain sensor to know how many days it hasn’t rained.
I have also tried to create a sensor that tells me the date of the last day that it rained and I can’t do it either. I’ve been searching the forum and can’t find anything that works for me. Could someone help me please?
There used to be a way to do it using the HIstory Stats integration, but it stopped working around June last year and, AFAIK, is still broken/bugged. Currrently, one way to do it is to use two template sensors.
template:
- trigger:
- platform: state
entity_id: binary_sensor.rain_detector
to: 'off'
from: 'on'
sensor:
- name: End of Last Rain
state: >
{{ now() }}
- sensor:
- name: Days Since Last Rain
state: >
{% if is_state('binary_sensor.rain_detector', 'on') %}
0
{% else %}
{{ (now() - states('sensor.end_of_last_rain') | as_datetime).days }}
{% endif %}
3 Likes
It works perfectly! Thank you very much for your help.
1 Like