Switch on the light, when sun goes down... But

… only, when I’m at home. That’s easy - two conditions in an automation.

BUT:
When I arrive at home after the sun is down, the light is off and the automation does not switch it on.

How would I configure, that a light is switched on when the sun goes down AND I’m at home
AND
if it is dark and I arrive later AND it is before 11 o’clock the light is switched on
AND
if it is 11 o’clock it is switched off.

Is there a way to configure this in Home Assistant?

I think I would build a binary template sensor which is on if all your conditions match and off if not.

You could add the time conditions into your template, but I often find it easer to just use a
TOD-sensor.

Something like this:

binary_sensor:
  - platform: tod
    name: Light on time
    after: sunset
    before: "23:00"

and your template sensor:

template:
  - binary_sensor:
      - name: Light auto
        unique_id: light_auto_binary
        state: >
          {{ is_state('person.hajo62', 'home') and states('binary_sensor.light_on_time') }}

binary_sensor.light_auto should now be on if you’re at home between sunset and 23:00 and off if one of the conditions are not true.
Then just automate your light against the true/false-state of your new sensor.

Tip: you can test your template in Developer Tools → Template

1 Like

Is this a real use case or would you like to understand how to combine a lot of things in one automation? Or where is the main problem in your requirement?

Either you go the way Skeletorjus suggested, which I do not like that much, because I cannot debug or see easy, when it is not happening as expected. But this is only a personal preference.

To your use case? Why not three automations? Each of them should be really simple. Or one automation with three triggers and then additional conditions in the triggger tree, e.g.

alias: Neue Automatisierung
description: ''
mode: single
trigger:
  - platform: sun
    event: sunset
    offset: '0'
    id: sun
  - platform: time
    at: '23:00'
    id: time
  - platform: state
    entity_id: person.foo
    from: not_home
    to: home
    id: home
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: sun
        sequence:
          - condition: state
            entity_id: person.foo
            state: home
          - service: light.turn_on
            target:
              entity_id: light.bar
      - conditions:
          - condition: trigger
            id: time
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.bar
      - conditions:
          - condition: trigger
            id: home
        sequence:
          - condition: sun
            after: sunset
          - condition: time
            before: '23:00'
          - service: light.turn_on
            target:
              entity_id: light.bar
    default: []

which is more or less three automations only in one. And yes, this could be more combined, AND, OR, default, … But with this simple approach, you have it easy to read and can use the fantastic automation log flow chart,

4 Likes

I’ve tried to translate with my sensors, but did not get it to fly.
Thanks for your solution. Unfortunately it seems to complicated for me at the moment, beside the problem, that my smartphone does not report a reliable “at home” state.

Looks like I could switch on and off in one automation, which was new for me.
I will now try to realize the easy two conditions mentioned in my question :innocent:

Did you try the visual way to have a look and adopt?

Click on new automation, in empty automation, switch directly via … to yaml mode, replace the empty code via paste of the whole code above and switch via … back to visual mode. Voilà. Yna d you only have to change the entities, numbers, … or remove one trigger, etc.

Yes, I have copied your example and tried to adopt with my devices. But it does not switch to on… I assume it’s cause by the presence detection. My smartphone is registered at home since 16 h although I been away for several hours,
(deveices: iPhone as devicetracker
But nevertheless, I would expect that running the automation (manually) before 11 o’clock should turn the light on?

alias: Außenlicht bei Sonnenuntergang
description: ''
trigger:
  - platform: sun
    event: sunset
    offset: '0'
    id: sun
  - platform: time
    at: '23:55'
    id: time
  - platform: state
    entity_id: device_tracker.iphone_8_hajo
    from: not_home
    to: home
    id: home
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: sun
        sequence:
          - condition: state
            entity_id: device_tracker.iphone_8_hajo
            state: home
          - service: switch.turn_on
            target:
              device_id: fbece82113e679c6a933b4d4d655xxxx
      - conditions:
          - condition: trigger
            id: time
        sequence:
          - service: switch.turn_off
            target:
              device_id: fbece82113e679c6a933b4d4d655xxxx
      - conditions:
          - condition: trigger
            id: home
        sequence:
          - condition: sun
            after: sunset
          - condition: time
            before: '23:55'
          - service: switch.turn_on
            target:
              device_id: fbece82113e679c6a933b4d4d655xxxx
    default: []
mode: single

For manual runs, only this conditions are skipped

condition: []

The other ones inside the actions are not skipped.

Did you have a look in the automation log flow chart
image

There you should easily see, what path was taken, what was the trigger, at which condistion it was stopped and what happened why.

Yes. It’s always the default path:

If you start is manually. Yes of course, because none of the trigger, which you are using in the conditionals has triggered. :wink:

My question was, what happened the last time, one of your triggers has triggered.

At least yesterday 23:55 is should have been triggered and switched of the light. Or yesterday at sundown (around 16:05) it should have been triggered and took the first path.

Makes the testing of automations a bit complicate, at least for a beginner like me :wink:
Today the light went on on sundown and went of at my selected time - at least when I’m at home. Perfekt.

Unfortunately I’ve been reported as at home the whole day, although I have been out two times today. The presence detection with my iPhone seems not to work :thinking:

Of course. But you’ll get this. as Here is nothing to test, this should not matter. If you have something to test, e.g. lights on or off, copy it temporarely in the default path and remove it afterwards.

How is it tracked? Via companion app? If yes and if you have a fritzbox, use this way

1 Like

If that is the Home Assistant iOS app, yes.

I will try with Fritzbox…

@Skeletorjus, I am a beginner. May I ask where do I put the binary_sensor: and template: chunks of yaml code? As part of the automation in automations.yaml file?

No, these are part of your configuration.yaml and not automation.

1 Like