Can a single automation turn a device on and off based on changing conditions?

Hello,
I set up an automation that would activate an outlet when the outside temperature was lower than the inside temperature. I’ve included it below and it seems to work so far. Is there a way for the same automation to turn the outlet off when the outside temperature goes back up? I could obviously create a second automation, but then I have to activate/deactivate two automations every time I don’t want this running. Plus it’s not as slick. :slight_smile: I’ve seen some examples that use templates, but they are calling a single service that sets a fan speed, whereas outlets appear to be an on service or an off service and and I’m not sure how to implement that.

id: '222222222222'
  alias: Turn Fan on With Temp
  description: ''
  trigger:
  - platform: template
    value_template: '{{ states.weather.home.attributes.temperature < int(states.sensor.lumi_lumi_weather_af4a0607_temperature.state)
      }}'
  condition:
  - condition: numeric_state
    entity_id: weather.home
    attribute: temperature
    above: '50'
  action:
  - type: turn_on
    device_id: 22222222222222222222222222222222
    entity_id: switch.outlet_bedroom1_fan_on_off
    domain: switch
  mode: single
  max: 10

It would be something like this:

id: '222222222222'
alias: Turn Fan on or off With Temp
description: ''
trigger:
- id: 'on'
  platform: template
  value_template: >
    {% set t = state_attr('weather.home', 'temperature') | int(0) %}
    {{ 50 < t < states('sensor.lumi_lumi_weather_af4a0607_temperature') | int(0) }}
- id: 'off'
  platform: template
  value_template: >
    {% set t = state_attr('weather.home', 'temperature') | int(0) %}
    {{ t > states('sensor.lumi_lumi_weather_af4a0607_temperature') | int(0) }}
condition: []
action:
- service: "switch.turn_{{ trigger.id }}"
  target:
    entity_id: switch.outlet_bedroom1_fan_on_off
mode: single
max: 10
  • It turns on the switch if the temperature is greater than 50 but less than the sensor’s value.
  • It turns off the switch if the temperature is greater than the sensor’s value.

If you don’t want it running for some short-term reason (or based on the behavior of something else in your system), consider adding a State Condition that monitors the state of an input_boolean. Turn off the input_boolean to disable the automation.

If you don’t want it running for some long-term reason, then use the automation’s existing enable/disable button.

Thanks! I’ll give this a try. So I’m creating two triggers and the id is established by the conditions and I’m using that as a variable to call the service. Clever. I didn’t know I could do this. :+1:

Yes I use an “Away” boolean to turn a lot of automations on/off when I leave, but sometimes it’s raining or already breezy or whatever and it’s nice to just disable with one button.