Boolean switch automation

Hi everyone,

I´m a beginner and I dont know if the following is even possible.
Long story short:
i want to combine 2 simple Automations into 1

alias: lighttest off
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.test
    to: 'off'
condition:
  - condition: state
    entity_id: input_boolean.test
    state: 'off'
action:
  - service: light.turn_on
    data:
      transition: 7
      color_name: red
    target:
      entity_id: light.wled_2
mode: single

and

alias: lighttest on
trigger:
  - platform: state
    entity_id: input_boolean.test
    to: 'on'
condition:
  - condition: state
    entity_id: input_boolean.test
    state: 'on'
action:
  - service: light.turn_on
    data:
      transition: 7
      color_name: green
    target:
      entity_id: light.wled_2
mode: single

so either the light is green or red.
I´m not sure if I´m completly wrong or if this isnt even possible.
(can do it with node red but it´s slow as hell…)

Could someone point out what to do or show me the tutorial i´m missing?

Thanks in advance & best regards
J

Welcome to the forum. :wave:

It is certainly possible with the choose action. https://www.home-assistant.io/docs/scripts#choose-a-group-of-actions It can also be done in a more compact way with templates, but as you are a beginner, let’s start with the easier to understand choose action:

alias: lighttest
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.test 
    to:
      - 'on' # trigger on either state. We could also leave to: out to trigger on all states but that might trigger on 'unavailable'
      - 'off'
action:
  - choose:
      - conditions: 
          - condition: state
            entity_id: input_boolean.test 
            state: 'off'
        sequence: # do this if the test input boolean is off
          - service: light.turn_on
            data:
              transition: 7
              color_name: red
            target:
              entity_id: light.wled_2
        default:  # do this if the test input boolean is not off (it must be on, because of the two trigger states specified)
          - service: light.turn_on
            data:
              transition: 7
              color_name: green
            target:
              entity_id: light.wled_2
mode: single

Another way to write it with choose

alias: lighttest
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.test  # no to: or from: specified -> trigger on all state or attribute changes of the entity
action:
  - choose:
      - conditions: 
          - condition: state
            entity_id: input_boolean.test 
            state: 'off'
        sequence: # do this only if the test input boolean is off
          - service: light.turn_on
            data:
              transition: 7
              color_name: red
            target:
              entity_id: light.wled_2
  - choose:
      - conditions: 
          - condition: state
            entity_id: input_boolean.test 
            state: 'on'
        sequence: # do this only if the test input boolean is on
          - service: light.turn_on
            data:
              transition: 7
              color_name: red
            target:
              entity_id: light.wled_2
mode: single

The template version would look like this:

alias: lighttest
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.test  
    to:
      - 'on' # trigger on either state. We could also leave to: out to trigger on all states but that might trigger on 'unavailable'
      - 'off'
action:
  - service: light.turn_on
    data:
      transition: 7
      color_name: "{{ 'red' if is_state('input_boolean.test', 'off') else 'green' }}"
    target:
      entity_id: light.wled_2
mode: single

You can read more about templates here: Templating

NOTE: listing trigger to: states like I did above only works in YAML and does not work in the UI automation editor. You have to use this instead:

trigger:
  - platform: state
    entity_id: input_boolean.test 
    to: 'on'
  - platform: state
    entity_id: input_boolean.test 
    to: 'off'

Hi Tom,

thanks a lot for the help; I didn´t see the choose action.

The 1st example throws an error :
Message malformed: extra keys not allowed @ data['action'][0]['choose'][0]['default']

The 2nd example only works once

and the 3rd example works like a charm and does exactly what i want.

Thanks a lot again for the advice and the provided examples.

Best regards
J

That’s because the default block is indented too far. It should line up with choose:. My mistake.

Can you see why?

It is a simple mistake in the sequence of choose actions you should be able to spot. Try to learn from this rather than just copying things you don’t understand.

Hi Tom,

I´m ashamed, yes I see now… should´ve seen it. I accuse a hard day of work for my blindness.

Thanks again :slight_smile:

I could say the same thing about my copying and pasting and forgetting to edit.