Binary Sensor change to trigger light switch on

Hi,
I am very new to using Home Assistant and I am trying to build an automation that does the following:

  1. Triggers 15 minutes after sunset.
  2. Then checks a binary sensor that has been set up to see if my TV is turned on.
  3. If the TV is on then turn on a Philips Hue light.

This is what I currently have but it isn’t working:

- id: '1573232716191'
  alias: TV_Light_TV_On
  description: This will automatically turn on the TV light if the TV is on after
    sunset
  trigger:
  - event: sunset
    offset: 00:15:00
    platform: sun
  condition:
  - condition: state
    entity_id: binary_sensor.living_room_tv
    state: 'on'
  action:
  - device_id: e1f5093088814aeb9a140cdd58a9c707
    domain: light
    entity_id: light.tv_light_2
    type: turn_on

Any ideas, I am completely new to this, I am sure it is something simple!
Thanks!

1 Like

Should be

    offset: "+00:15:00"

See here for more info.

I actually don’t see anything wrong. Why do you say it doesn’t work? Do you see any errors in the log? Have you tried manually triggering the automation to see if it turns on the light? Have you checked the state of binary_sensor.living_room_tv at 15 minutes after sunset to see if it is on? Are you sure the automation is turned on?

I don’t think that’s necessary. Although it’s usually better to quote times like this, in this case I don’t think it will make a difference. Also the plus sign isn’t required (but doesn’t hurt either.)

Through trial and error testing, I am pretty certain the error is to do with the binary sensor, rather than the trigger or the action. I have tested the rest and it seems to work. For whatever reason it doesn’t seem to acknowledge the condition with the binary sensor :confused:

Good to know, thanks.

@bbarnes1987 The condition looks right, so I’d check if the binary sensor is on at the time that the automation should fire (like Phil said).

What do you mean by that? If you have the entity_id entered correctly (i.e., no typo), and it’s on at 15 minutes after sunset, then it should work.

Just for testing purposes, and to make sure, e.g., your clock and time zone settings are correct, have you tried removing the condition to see if it will trigger at the correct time?

Whats the easiest way to check the binary sensor is on, I am waiting until it shows connected? It turns the light on when connected, but also turns it on when disconnected.

Developer Tools > States. Find the entity ID there

Great thanks, I have checked that, the state is definitely changing from on to off when the TV is on or off.

Just changed the trigger from sun trigger to a time trigger and it works fine, so it must be to do with the sun trigger, the binary sensor is obviously working.

Sorry, but how do you know when sunset + 15 minutes is for your location? (Or, more importantly, when HA thinks the sunset is.) Have you checked the sun.sun component? Also, how do you know that the binary sensor was on at 15 minutes past sunset? Have you checked the logs?

You can try putting quotes around the offset time, but I don’t think that will make a difference. For testing purposes you can just temporarily change the offset so that it will trigger in the next minute or so (enough time to restart HA for the test.)

EDIT: So, about the quotes around the time in the offset parameter. There are some places where quotes are required around times, and in other places it doesn’t seem to matter. I’ve never really gotten to the bottom of that, so maybe adding quotes is worth a try, just to rule that out as the problem. :man_shrugging:

Ok, so I have decided I have the conditions and triggers round the wrong way for what I want, as if the trigger was 15 minutes after sunset it would only check this once. What I need is when the TV is turned on to check if the sun has set and then turn the light on. That could obviously happen any number of times in a night. So I have changed it and got it to here:

- id: '1573232716191'
  alias: TV_Light_TV_On
  description: This will automatically turn on the TV light if the TV is on after
    sunset
  trigger:
  - entity_id: binary_sensor.living_room_tv
    for: '2'
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - before: sunrise
    condition: sun
  - after: sunset
    condition: sun
  action:
  - device_id: e1f5093088814aeb9a140cdd58a9c707
    domain: light
    entity_id: light.tv_light_2
    type: turn_on

Without the condition this works fine now, however I can’t seem to get the condition right to say do it if it is after the sunsets but before it rises, any ideas?

Common mistake. The way you currently have the condition it would have to be before sunrise AND after sunset, which it can never be. Try replacing with:

  condition:
    condition: state
    entity_id: sun.sun
    state: 'below_horizon'

EDIT: If that’s not exactly what you want then check out the various ways to use the sun condition.

To expand on that OP, multiple conditions are AND by default (which is implied by what Phil said, but figured I’d share).

ok that’s great, changed it and it work. I then thought I would test it to make sure it didn’t turn on if it was “above horizon” so just changed below to above and it still turned the light on, any more ideas?

Are you sure you’re restarting HA when you change your automation?

Is a full reboot required?

It depends on how your configuration is laid out. If things aren’t working as expected, chances are you need to restart HA. You can do that from the Configuration -> Server Control page. You shouldn’t need to reboot the entire system.

restart has done the trick, thanks for all your help!

2 Likes