Automation to turn switch off or on depending on current state

Today it was windy enough I had an inflatable get blown over so I want to setup an automation that if the wind is over 15 it shuts off the light switch that controls power to the outlet, but if it’s below it turns the switch back on. Is there a better way to do this?

Current:

  • Trigger: When wind value is above 15
  • Then Do: Toggle light switch

What I’d like to do is if the wind is over 15, turn off, and when it drops below turn on. Is there a way to have branches based on each condition? Something like the below:

  • Trigger: Entity Wind speed or wind gust changes
  • Condition: Wind speed is below 15 AND outlet is off
    ** Action: Turn on outlet
  • Condition: Wind speed is above 15 AND outlet is on
    ** Action: Turn off outlet.

Hopefully this makes sense. Do I need two automations to do this or can I do this somehow in one?

Looks like if I skip the condition and use the “tehn do” block to do a building block I can fork out inside there. I haven’t been able to find this directly in my searching but… if i do the below steps, will this basically run every time the wind speed on the entity is updated?

  • Click Add trigger
  • Click Entity
  • Click State
  • Select correct entity
triggers:
  - platform: state
    entity_id: sensor.wind_speed
actions:
  - action: homeassistant.turn_{{ 'on' if trigger.to_state.state|float(100) < 15 else 'off' }}
    target:
      entity_id: switch.outlet

Comments:

  • triggers off any change to the wind speed sensor. I’ve guessed its entity ID because you didn’t include it.
  • turns the outlet on if the wind speed is below 15, otherwise off.
  • I’ve used the generic homeassistant.turn_xxx action in case your outlet is not a switch. I had to guess its entity ID because you didn’t include it.
  • I’ve used |float(100) on your speed sensor as a fail-safe: if, for whatever reason, it reports unavailable or unknown, it assumes a high wind speed as a default.
  • I’ve assumed that it doesn’t matter if you attempt to turn on the switch when it’s already on, or similarly for off.

I’d recommend handling this like a thermostat - with some delta between on and off. Otherwise when the win oscillates around 15, your switch is not going to be happy. For example:

trigger: speed > 15
action: turn_off

trigger: speed < 13
action: turn_on

Also, there is no need to check for state of the outlet. No harm turning off an outlet that is already off, or turning on when it’s already on. It only makes it less readable.

I should have included the YAML, Sorry @Troon and @tomas1 here’s the yaml view.

With this setup, let’s say I’m going to be gone for a few days and I want to turn this off. Whenever the entity updates (Accuweather integration) with wind under 15, it would cause the light to turn off as I understand it. This is a Leviton light switch that “drives” the power to the outlet, and since lights cannot be retyped I have to use that dont let it throw you.

To Tomas’ point, using a delta is likely a good idea I’ll probably have to work that in somewhere so it will smooth out off/on cycling (the entity refreshes every 5 minutes from looking at the history).

Also, is it possible this runs only between two dates? Ex: 10/1 through 1/14. I’m not finding how to do that in Lovelace.

description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.weather_wind_speed
conditions:
  - condition: template
    value_template: "{{ (10,1) <= (now().month, now().day) <= (1,14) }}"
    alias: Oct-1 through Jan 14
actions:
  - if:
      - condition: or
        conditions:
          - condition: numeric_state
            entity_id: sensor.weather_wind_gust_speed
            above: 15
          - condition: numeric_state
            entity_id: sensor.weather_wind_speed
            above: 15
    then:
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: light.soffit_outlet
    else:
      - action: light.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: light.soffit_outlet
mode: single

Not sure that’ll work across year boundaries. Try:

    value_template: "{{ now().month > 9 or (now().month == 1 and now().day < 15) }}"