I want to automate my pool and need timers for the switches to record how long they were on and off.
I would then like to use these to control my automation with conditions.
This is working so far for example:
- trigger:
- platform: state
entity_id: switch.pool_pump_switch
from: 'on'
to: 'off'
sensor:
- unique_id: pool_pump_time_off
name: "test1 pool pump off"
state: "{{trigger.to_state.last_changed}}"
device_class: timestamp
However, I would need the values ââin seconds since the switch was last in the on or off state.
Is there a way to do this?
Hi, cant test now, but you can try as below. BUT, just to mention I would prefer a history stat for this rather than a trigger, but i guess it is personal preference.
Thats a good question.
If I can use the sensor with the unique id âpool_pump_time_offâ from the example as âsensor.pool_pump_time_offâ, than the name is correct.
If I put your example in dev tool, template I get:
ValueError: Template error: as_timestamp got invalid input âunknownâ when rendering template â{{ states(âsensor.pool_pump_time_offâ) }} {{ states(âsensor.pool_pump_time_offâ) | as_timestamp }}â but no default was specified
Because you configured its device_class to be timestamp but the sensorâs state value is a value in seconds which doesnât meet the requirements of a timestamp sensor (i.e. the value must be a datetime in ISO format, such as what you originally had trigger.to_state.last_changed).
If you want to express the value as a number in seconds, you will need to remove device_class: timestamp
Maybe Iâm just adding more noise to this thread, but itâs possible that the information you need is already present in the state machine and therefore already available for use in an automation (and therefore you donât need to create any new sensors to store anything).
It would be useful to understand what triggers you plan to use for your automation and what information you need about the pumpâs history.
When your automation runs, you will be able to determine the current state of the pump (obviously) and also determine how long it has been in that state.
If you trigger on the state change of the pump, you know it will have been in the new state for 0 seconds, but you will also have trigger data to know how long it had been in the previous state.
Wow, thank you very much.
This helps me am lot for this project and I have a good idea to implement it.
But are the two things possible with the history stat:
restart the measurement after every switch, that every cyle starts with 0
the meaurement is not only updateted, when the state of the switch is changed
it should update for example every second, when the state is for example on
If you setup the history stat the way I proposed, then
every day will start at 0, not every cycle. if you rather want to measure cycle, you can change the history stat template code to look at current cycle, or just use the trigger data as sugested by @mekaneck
it will be a counter for the day. It will update continuously and not only when state is changed. Not every second, but at least every minute or two, and for me that is sufficient enough for automation purposes.
Thank you, this works exactly as you described it.
One more question. Is it possible to see the code that the gui has generated?
I think this would help me for better understanding working in yaml myself.
Not that I am aware of.
It might have created some json config options in the .storage folder, but I would not suggest going there. Rather read the docs and forum if you want to learn yaml.