Light only when TV is on after sunset

Im working on an automatiion to turn on lights when TV is on AND sun is below elevation, but somehow my conditions does not work. What am I doing wrong?

alias: TV - LED light ON
description: ""
trigger:
  - platform: device
    device_id: f80f3241f926d57335a8ca03a716956d
    domain: media_player
    entity_id: media_player.lg_webos_smart_tv
    type: turned_on
    id: tv_on
  - platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: 5
    id: sun_down
condition:
  - condition: trigger
    id:
      - sun_down
      - tv_on
action:
  - type: turn_on
    device_id: f4d89f57d049e390aae7396db8d85b97
    entity_id: 3b27a5dc20112b738917ea4949590c2f
    domain: light
mode: single

I don’t believe this exists as a condition.

Also, are you sure you want sunset as a trigger?

Hi, I think your trigger is already wrong.

The automation should be evaluated (triggered) all the time by the changing of the elevation.

platform: state
entity_id:
  - sun.sun
attribute: elevation

The condition is the TV being ON AND the sun below 5*, if true, the action processed.

condition: numeric_state
entity_id: sun.sun
attribute: elevation
below: 5

As you describe it, you haven’t got a trigger at all - you want the lights to come on if the TV is on and if the sun’s elevation is below 5 - both conditions - but there is no event to kick the process off.

Is this a good idea?

How about a template binary sensor - “on” if the TV was on and the sun’s evelation was below 5, otherwise “off”? It’s state would be evaluated every time either one changed and would provide a simple state (not device_id) trigger for the automation.

Your condition is just checking the trigger IDs. Since you have both listed, then it’s not doing anything. I would change the conditions to be an AND with both trigger conditions being met.

condition:
  - condition: and
    conditions:
      - condition: device
        device_id: f80f3241f926d57335a8ca03a716956d
        domain: media_player
        entity_id: media_player.lg_webos_smart_tv
        type: is_on
      - condition: numeric_state
        entity_id: sun.sun
        attribute: elevation
        below: 5
1 Like

Why not?
I did something similar for automatic curtains, the sun’s azimuth and temperature inside.
Not saying this is the best/ideal way and I’m always open for learning something new. :wink:

Conditions are always AND

Unless you use an or…of course.

Are you refering to the trigger in conditions? Why shouldn’t they trigger as they are refering to the initial triggers which Ihave verified is working.
My problem is that conditions are not doing its job…

Thanks man it did the job.

Maybe I misunderstood how the trigger IDs are working. It just seemed to be a good idea to make a reference instead of doing the setting again.

I don’t think it will because the automation will not get triggered when you switch on the TV AFTER the sun has gone below 5° + you are learning something wrong: you don’t have to AND conditions since they are AND by default.

Trigger ID’s are meant to use together with actions when you have multiple triggers in the same automation, for instance a motion detected light where you use the detection from the sensor switching on or off.

I’m sure it works, it’s just that I ran a short test this morning and the automation would have been triggered about 100 times in the space of a few hours:

Must be consuming resources, surely? Not to mention wear and tear on the SD card. Continuous evaluation of a state is what templates are for, aren’t they? Or maybe in this case, a threshold helper. :thinking:

1 Like

Correct. That was my mistake. Just adding both as conditions would work.

How so?

You’re right, that seems to be a better solution. :+1:
I watched the release party but I couldn’t see how to use it - until now! :star_struck:
What would then be the trigger in this case if you put TV and SUN in the sensor

I was wrong.
I wonder though if, by putting both as a trigger and condition is ‘the best’ solution.

All good, just wanted to make sure I didn’t miss something.

I suppose it’s 6 in one hand and a half dozen in the other. The one benefit of my proposal, is that it’s configurable from the UI if someone isn’t comfortable diving into templating.

Well… if the sensor were:

template:
  - binary_sensor:
      - name: "lights on"
        state: >
          {% if is_state('media_player.lg_webos_smart_tv', 'off') %}
            off
          {% elif is_state('sun.sun', 'above_horizon') %}
            off
          {% else %}
            on
          {% endif %} 

Then the automation trigger would be:

platform: state
entity_id:
  - binary_sensor.lights_on
to: "on"

Is that what you mean?

1 Like