I hope you can help me with a project:
I’d like to see how long it’s been since I’ve pressed a button.
I’m planning an irrigation control. If I manually activate a switch, I would like to activate a kind of timer that counts until I turn the switch off again.
The problem you may run into is HA restarts. I think it would be better to save a timestamp in an input_number (whose value will be restored across restarts) when you activate the switch. Then you can tell how long it has been by subtracting that from the current timestamp. The details will probably depend on exactly what you’re trying to do, since your description is somewhat vague.
E.g.:
- alias: record time when switch is activated
trigger:
platform: state
entity_id: switch.xyz
to: 'on'
action:
service: input_number.set_value
entity_id: input_number.switch_activated
data_template:
value: "{{ as_timestamp(now()) }}"