Automation Trigger Switch

Hello,

I’ve made an automation to trigger a switch between a time period when one of us gets home, or if we are home when the sun sets.

Strangely it doesn’t get triggered when I get home after sunset.

# Lichten aan bij zonsondergang of wanneer iemand thuis tussen 16.00-23.00
- alias: 'Keukenverlichting aan bij thuis of thuiskomst'
  trigger:
    - platform: sun
      event: sunset
      offset: '-00:00:00'
    - platform: state
      entity_id: device_tracker.iphonemaurice
      to: 'home'
    - platform: state
      entity_id: device_tracker.iphone_van_yvette_2
      to: 'home'        
  condition:
    - condition: state
      entity_id: device_tracker.iphonemaurice
      state: 'home'
    - condition: state
      entity_id: device_tracker.iphone_van_yvette_2
      state: 'home'
    - condition: time
      after: '16:00:00'
      before: '23:00:00'
  action:
    service: switch.turn_on
    entity_id: switch.sonoff_voortuin

I just copied and edited a full written example from the Hassio website:

Automation Examples

Any suggestions?. I can’t figure out what’s wrong with it.

It’s probably because not all conditions are satisfied, when you get home. Please be aware that conditions are AND by default. So the switch will only turn on if you are home AND the other person is home AND it’s between 16 - 23.

The example you edited uses a group in the trigger and in the condition and the group shows home when at least one person is home.

Either group your device trackers or make multiple automations.

Your conditions require that both of you are home for this automation to complete. Is that what you intend? One of you arriving home when the other is not wouldn’t satisfy the conditions. Note that you can use “or” logic in conditions (default is and).

it just got triggered when the wife came home ;).
Will try to sort the thing out. Thanks

Yup. I bet the state of device_tracker.iphonemaurice was already home

It indeed was. Thats my phone.

How can I get the OR in it?. Do I need to change the - platform to - condition? or ?

Check out that link in my response. It gives a n “or” example. Getting the syntax right the first time can be a bit tricky - use the “check config” button on the server control page after you make changes to confirm you’ve got it right.

Okay. So Hassio says it’s valid. But is it?.

# Lichten aan bij zonsondergang of wanneer iemand thuis tussen 16.00-23.00
- alias: 'Keukenverlichting aan bij thuis of thuiskomst'
  trigger:
    - platform: sun
      event: sunset
      offset: '-00:00:00'
    - platform: state
      entity_id: device_tracker.iphonemaurice
      to: 'home'
    - platform: state
      entity_id: device_tracker.iphone_van_yvette_2
      to: 'home'        
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: device_tracker.iphonemaurice
        state: 'home'
      - condition: state
        entity_id: device_tracker.iphone_van_yvette_2
        state: 'home'
      - condition: time
        after: '16:00:00'
        before: '23:00:00'
  action:
      service: switch.turn_on
      entity_id: switch.sonoff_voortuin

This automation essentially says:
If one of these thing happens:

  • sun sets
  • Maurice comes home
  • Yvette comes home

then turn on the switch as long as one of these things is true:

  • Maurice is home
  • Yvette is home
  • The time is between 4pm and 11pm

Do you really want the “someone is home” part of the condition?

Yeah I would like that part. But merely I would like to turn on the switch if one of us comes home after the sun het set. So I think it’s correct but I need to remove the “Time is between” part.

If you do that (removing the time condition), the light will come on every time one of you arrives home.

And if you come home between 4-11, the light will come on even if the sun hasn’t set.

That condition should probably be based on sunset, too.

You can combine “and” and “or” conditions. Here’s an example.

You’ll want “after sunset AND one of us is home”

1 Like

Hmm difficult.
What do I need to put were?.

Need to put the time as a an AND condition. But how to build that :neutral_face:

Just like that example I linked to. This should work (“check config” to be sure I’ve got syntax correct)

condition:
  - condition: state
    entity_id: sun.sun
    state: above_horizon
  - condition: or
    conditions:
      - condition: state
        entity_id: device_tracker.iphone_van_yvette_2
        state: 'home'
      - condition: state
        entity_id: device_tracker.iphonemaurice
        state: 'home'

This condition is true as long as the sun is below the horizon and one of you are home.

Alright.

I edited it a bit yesterday. YAML validator says it’s valid, but when reloading my automations it’s not…

# Lichten aan bij zonsondergang of wanneer iemand thuis tussen 16.00-23.00
- alias: 'Keukenverlichting aan bij thuis of thuiskomst'
  trigger:
    - platform: state
      entity_id: device_tracker.iphonemaurice
      to: 'home'
    - platform: state
      entity_id: device_tracker.iphone_van_yvette_2
      to: 'home'        
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
        offset: '-00:30:00'
  action:
      service: switch.turn_on
      entity_id: switch.shelly_shsw_1_c42b0b

Invalid config for [automation]: extra keys not allowed @ data[‘condition’][0][‘conditions’][0][‘offset’]. Got None
extra keys not allowed @ data[‘condition’][0][‘conditions’][0][‘state’]. Got None
not a valid value for dictionary value @ data[‘condition’][0][‘conditions’][0][‘condition’]. Got None. (See /config/configuration.yaml, line 12). Please check the docs at https://home-assistant.io/integrations/automation/

Right - you can’t use “offset” with the “below_horizon” state. Just remove that line.

Just figured that out.

Needed to replace the -Condition: State with - Condition: Sun

    conditions:
      - condition: sun
        after: sunset
        after_offset: '-00:30:00'

The way I suggested (condition: sun below horizon) lines up the sun condition with the sun trigger. So the light will only come on at sunset or later.

If you use your condition, the light will come home if you arrive within 30 minutes of sunset (or after sunset). Which is probably a good way to do it. I like to have my lights come on before sunset since it gets dark inside before the sun is below the horizon.