Hi,
I would like to show on lovelace the date and time of the last time it rained using weather.home entity.
I have been looking in the forum for some solutions, but they only show the last time a sensor changed state. Anybody can suggest a solution?
Thank you!
There are 3 valid rain states.
‘lightning-rainy’
‘snowy-rainy’
‘rainy’
Create an input_datetime
input_datetime:
last_rain:
name: Time of last reported rainfall
has_date: true
has_time: true
Now an automation that triggers when one of those states is reported
trigger:
platform: template
entity_id: weather.home
# True if the state contains the word 'rainy'
value_template: "{{ 'rainy' in states('weather.home') }}"
action:
service: input_datetime.set_datetime
entity_id: input_datetime.last_rain
data_template:
datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
Now whenever it rains, this input_datetime will be the last time it started raining.
Note, if you want to know when it last stopped raining, you’d have to change the trigger to trigger when it changed from ‘rainy’ to anything else. Maybe just trigger on all changes and a condition that checks if the to_state or from_state contains the word ‘rainy’
I’m actually not sure if you can use trigger.from_state inside the trigger itself. I’ve never tried it. But I’ve used it in the condition, so this should work.
trigger:
platform: state
entity_id: weather.home
condition:
# Will only run the action if the previous state contained the word 'rainy' or the
# next state contains the word 'rainy'
condition: template
value_template: "{{ 'rainy' in trigger.from_state.state or 'rainy' in trigger.to_state.state }}"
action:
service: input_datetime.set_datetime
entity_id: input_datetime.last_rain
data_template:
datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
Now the value will update as soon as it starts raining and as soon as it stops raining.
@jocnnor, thank you for the very detailed explanation, this is a very precious learning point!
I’ll definitely be able to reuse this also in other scenarios
I’ll test it out this weekend!
@jocnnor amazing, it worked!
I have tried to adapt the same code to remind myself the last time I have watered the plants (I have a switch on hass that turns on the irrigation), but it seems that I cannot make it work!
This is the input_datetime:
input_datetime:
last_irrigation:
name: Last irrigation ended on
has_date: true
has_time: true
and this is the automation I set-up:
- id: '1582125880512'
alias: Last irrigation2
description: ''
trigger:
- entity_id: switch.balcony_irrigation
from: 'Off'
platform: state
to: 'On'
condition: []
action:
- data_template:
datetime: '{{ now().strftime(''%A, %b %d at %H:%M'') }}'
entity_id: input_datetime.last_irrigation
service: input_datetime.set_datetime
I have also tried this automation:
- alias: Last irrigation
trigger:
platform: state
entity_id: switch.balcony_irrigation
condition:
condition: template
value_template: '{{ ''off'' in trigger.from_state.state and ''on'' in trigger.to_state.state
}}'
action:
service: input_datetime.set_datetime
entity_id: input_datetime.last_irrigation
data_template:
datetime: '{{ now().strftime(''%A, %b %d at %H:%M'') }}'
id: 013987121388413698c1f4bb3268420e
You’ll have to use the exact strftime string I used before. The input_datetime needs a string in a specific format to be able to set it.