Turn off lights at specified time and TV is off


#1

Hi I would like it so my lights are always turned off in my Living room at 22:30; but if my TV is on then they should stay on until the TV is switched off and upon my TV being switched off they are then switched off. My TV is picked up on the device tracker and, and so it has a state of ‘home’ and ‘away’; I have been able to get a simple timed event to switch my lights on and off and I can see the state change in device tracker of my TV, but I can’t get an automation to work, this is what I have so far, but doesn’t work:-

 - alias: "Timed rule and state turn off living room lights after 22:30 and TV off"
   trigger:
     - platform: time
       at: "22:30:00"
     - platform: state
       entity_id: device_tracker.sonybraviatv
       to: 'away'
   condition:
     - condition: state
       entity_id: device_tracker.sonybraviatv
       state: 'away'
     - condition: time
       after: '22:30:00'
   action:
      service: homeassistant.turn_off
      entity_id: group.living_room

Any help would be much appreciated.


#2

That should work. Are you sure the automation is turned on? Check it on the states page.

Also, why are you using a device_tracker for your TV? I would suggest a ping binary_sensor instead. On the other hand, if it’s not broken…


#3

I have checked the Logbook and it shows that it has “fired” but doesn’t seem to have done anything. Strangely other automation’s are firing to, ones that I would expect to be firing as they are for automation’s that should happen tomorrow morning.


#4

Take it step by step. First, try calling the homeassistant.turn_off service with entity_id of group.living_room from the Services page and see if it turns off the lights as expected.

If that works, then try manually triggering the automation. Do that by finding it on the States page, then click the little box with the arrow in it, then in the window that pops up, click on the TRIGGER button. Does that turn off the lights as expected?

Next it sounds like you might have a configuration issue with the clock. Try entering the following in the template editor:

{{ utcnow() }}
{{ now() }}
{{ utcnow().astimezone() }}
{{ now().astimezone() }}
{{ utcnow().tzinfo }}
{{ now().tzinfo }}
{{ now().astimezone().tzinfo }}

The first line should show the current time as expressed in the UTC time zone.
The second, third and fourth lines should show the current time as expressed in the local time zone.
The fifth line should show UTC.
The sixth and seventh lines should indicate the local time zone (probably in two different formats.)


#5

Thanks. All three steps (all of which I have not used before, so a useful learning for me) all work as expected. The time stuff also checks out i.e. the time is correct for my timezone. Thanks for your help, I will pick this topic up sometime in the next couple of days.


#6

Further research into this and indeed the actual rule I posted worked correctly as advised by pnbruckner. The problem was down to the fact that in my automations.yaml file I had defined many automations none of them with an scalar of id: I had just used an alias:-
So the correct format is:

 - id: Rule4
   alias: "Timed rule and state turn off living room lights after 22:30 and TV is off"
   trigger:
     - platform: time
       at: "22:30:00"
     - platform: state
       entity_id: binary_sensor.sonybraviatv
       to: 'off'
   condition:
     - condition: state
       entity_id: binary_sensor.sonybraviatv
       state: 'off'
     - condition: time
       after: '22:30:00'
   action:
      service: homeassistant.turn_off
      entity_id: group.living_room

basically adding - id: and giving it a unique value fixed the miss firing triggers. As advised by pnbruckner I have also changed the testing of the state of my TV from a device_tracker to a ping binary_sensor, I must admit using the ping binary_sensor means you can configure the checking time for just this device rather than affecting the device_tracker schedule.