I’m trying to make a script to turn on my pool pump when the temperature get below freezing and turn it off when it gets above freezing. I can run 2 automations but I’d like to do 1 script instead. Anybody have any scripts they can send me doing something similar?
I was just re-reading your question here after you sent me a private message.
If you are just after a single automation that turns on and off the pump based on the temperature, then the easiest way is probably to have the automation trigger by temperature state change, and in the action use a service template to turn on/off the pump. So, something like this:
automation:
trigger:
platform: state
entity_id: sensor.pool_temperature
action:
- service_template: >
{% if states.sensor.pool_temperature.state < 0 %}
homeassistant.turn_on
{% else %}
homeassistant.turn_off
{% endif %}
data:
entity_id: switch.pool_pump
Close, but a state is always a string, so comparing without first converting to a number won’t work. Also, when turning a switch on and off, although you can use the homeassistant services, it’s better to use the corresponding switch service (it’s more efficient.) Lastly, automations require an alias. So:
automation:
- alias: Turn pool pump on and off
trigger:
platform: state
entity_id: sensor.pool_temperature
action:
service_template: >
{% if trigger.to_state.state|float < 0 %}
switch.turn_on
{% else %}
switch.turn_off
{% endif %}
entity_id: switch.pool_pump
1 Like
Thanks for the corrections @pnbruckner.