How to write a single automation for low/high temp triggers with different actions?

I have 2 separate automations to check sensor temp in my kid’s room to turn on/off a space heater. I’m doing this because I couldn’t figure out how to combine them into a single automation with different actions based on the below/above trigger values.

In other words, I have this:

Automation 1: Too cold

TRIGGER:

ACTION:

Automation 2: Too hot

TRIGGER:

ACTION:

I tried to combine them at first, but saw no way of action the “Action” to map the turn on/ff service call correctly. I considered using the “toggle” service, but that was more opaque to me.

Is there a way to combine this that I don’t know or am I doing it the right way?

You can’t do it in the ui - this needs a service template, which is ‘advanced’. The point of the ui is to quickly get working automations, and not worry about the underlying code. Based on that philosophy, having two automations is fine.

If you specifically want to use only one, you’ll need to edit the yaml, which will look something like

trigger:
  - platform: numeric_state
    entity_id: sensor.kids_room_temperature
    below: 71
  - platform: numeric_state
    entity_id: sensor.kids_room_temperature
    above: 77
action:
  service: "switch.turn_{{ 'on' if states('sensor.kids_room_temperature')|float < 71 else 'off' }}"
  entity_id: switch.kids_heater
1 Like

Looks like you can do it in the UI, if I turn YAML mode on within the UI for the specific trigger. After saving it, the UI changed it to this:

Thanks!

1 Like

I think you can do this in the UI with the new Choose action.

Trigger on any temperature change:

Option 1: Too Hot

Option 2: Too Cold:

I don’t have a heater in my kids’ bedroom, so I used a light for the action… but you get the idea.

The resultant YAML looks like this:

  alias: Heater Control Example
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.kid_bedroom_window_temperature
  condition: []
  action:
  - choose:
    - conditions:
      - condition: numeric_state
        entity_id: sensor.kid_bedroom_window_temperature
        above: '77'
      sequence:
      - service: light.turn_off
        data: {}
        entity_id: light.kid_bedroom_fan_light
    - conditions:
      - condition: numeric_state
        entity_id: sensor.kid_bedroom_window_temperature
        below: '71'
      sequence:
      - service: light.turn_on
        data: {}
        entity_id: light.kid_bedroom_fan_light
    default: []
  mode: single
4 Likes

Btw, this worked using the "“Choose” action. Thank you!

Morning, i adopted the yaml for my automation. But i am not recognizing when this was triggered then.
I set the values that it must happen something. But the trigger seems not to be triggered by HA.

Any ideas? :slight_smile:

ok, just got it