How can i use the outcome of a condition as a state for a device in an automation?

Hi,

I am making an automation that has to switch devices on and off based on presence and the state of the sun.

Instead of making one for switching on and one (or more) for swiching off, I was wandering if i could do it with only one, were I use the outcome of the condition as a state, switch_on when true, and switch_off when not true.

This is what I have so far:
whenever one of the 3 entity’s changes state it detects that and check’s if the conditions passe or not
that works fine, only think I do not know is how to turn devices off, if conditions are not passed.

alias: test
description: ""
trigger:
  - platform: state
    entity_id:
      - device_tracker.telefoon_dick
      - device_tracker.telefoon_esther
      - sun.sun
condition:
  - condition: and
    conditions:
      - condition: or
        conditions:
          - condition: state
            entity_id: device_tracker.telefoon_dick
            state: home
          - condition: state
            entity_id: device_tracker.telefoon_esther
            state: home
      - condition: sun
        before: sunset
        after: sunrise
action:
  - service: switch.   # turn on when condition is met, turn off when not!
    data: {}
    target:
      entity_id: switch.mf_fountain_on
mode: single

I would suggest the following approach:

Remove everything from the conditions section of the automation.

Then, use an if-then-else action. The condition for this can be your original condition. The ‘else’ actions will handle what happens when your condition is not met.

2 Likes

Listen to templrton_nash.

If the conditions don’t pass the actions are not run. So there is no way to turn off the light.

2 Likes

Many thanks, for pointing me in an other direction!

Couldn’t get the if then else to work though, but your suggestion did work fine with the choose statement.

alias: Fontijn Automatisch
description: ""
trigger:
  - platform: state
    entity_id:
      - person.person1
      - person.person2
      - sun.sun
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: or
                conditions:
                  - condition: state
                    entity_id: person.person1
                    state: home
                  - condition: state
                    entity_id: person.person2
                    state: home
          - condition: sun
            after: sunrise
            before: sunset
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.mf_fountain_on
            data: {}
    default:
      - service: switch.turn_off
        target:
          entity_id: switch.mf_fountain_on
        data: {}
mode: single

That AND condition is not doing anything, you can remove it. Conditions are AND by default, and your indentation is not quite correct.

action:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: state
                entity_id: person.person1
                state: home
              - condition: state
                entity_id: person.person2
                state: home
          - condition: sun
            after: sunrise
            before: sunset
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.mf_fountain_on
            data: {}
    default:
      - service: switch.turn_off
        target:
          entity_id: switch.mf_fountain_on
        data: {}

This will be (person1 OR person2 home) AND during the day.

If you wanted to leave the AND condition in it should be indented like this:

action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: or
                conditions:
                  - condition: state
                    entity_id: person.person1
                    state: home
                  - condition: state
                    entity_id: person.person2
                    state: home
              - condition: sun
                after: sunrise
                before: sunset
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.mf_fountain_on
            data: {}
    default:
      - service: switch.turn_off
        target:
          entity_id: switch.mf_fountain_on
        data: {}
1 Like