I’m trying to set up an automation that turns on a smart switch when one of my TV’s turn on, but I can’t seem to figure out what I’m doing wrong. I use the brightness setting of a light in the same room as a trigger:
- id: '1538927914592'
alias: TV on plug on (Attick)
trigger:
- platform: numeric_state
entity_id: light.attick
value_template: '{{ state.attributes.brightness }}'
below: 70
for:
seconds: 4
condition:
- condition: state
entity_id: media_player.sony_XXXX
state: 'on'
- condition: state
entity_id: media_player.panasonic_XXXX
state: 'on'
- condition: state
entity_id: sun.sun
state: below_horizon
action:
- entity_id: light.onoff_plug_1
service: light.turn_on
I want the plug to turn on when one of my tv’s turns on, but as I set it up right now that doesn’t seem to work.
I tried changing the conditions to:
condition:
condition: or
conditions:
- condition: state
entity_id: media_player.sony_XXXX
state: 'on'
- condition: state
entity_id: media_player.panasonic_XXXX
state: 'on'
- condition: and
conditions:
- condition: state
entity_id: sun.sun
state: below_horizon
Edit: I’ve set up an automation that lowers the brightness when the tv turns on btw. The setup works if I leave out one tv.
I hope someone can help me out. Thanks in advance for any help.
@anon43302295 - yeah, you were a bit faster as well!
Though for the extra two lines: I prefer to make everything explicit, sometimes it facilitates modifications later. And it is definitely better for people who just start writing complex conditions.
FYI using and on the top level of condition have caused problems for many people. You really should go with @anon43302295’s solution if you want to avoid future headaches.
I’ve just seen automations flat out not work with an and condition. I’ve worked with at least 3 people in the last year who had the correct yaml but the automation didn’t work. As soon as
condtion: and
conditons:
was removed, the automations worked.
This may be purely anecdotal, but it’s what I’ve seen on more than one occasion.
EDIT: This only applied to the top level:
condition:
# This is the top level I'm refering to.
- condition: and
conditions:
# Second level, works fine.
- condition: and
condtions:
# Third level, never tried.
- condition: state
To be fair, there’s no reason it shouldn’t work and I suspect that the people you’ve helped have had indentation problems with the top level and probably other syntax problems that you’ve fixed along the way.
But I definitely think less is more when it comes to this kind of thing and fewer lines in the configuration is fewer places for mistakes to creep in
I also have this condition and it works. But it’s good to know that the default action for conditions are “and” and it doesn’t need to be explicitly written. Thank you.
condition:
condition: and
conditions:
- condition: state
entity_id: switch.wall_switch_xxxxxxxxxx
state: 'off'
- condition: or
conditions:
- condition: time
weekday:
- tue
- wed
- thu
- fri
- sat
- sun
- condition: time
before: 09:00:00
after: '15:00:00'
But it doesn’t like the number 22 for some reason. Do I have to use quotes maybe or other punctuation marks?
Edit:
And is it better to explicitly create a condition with the original automation that states that the plug I’m triggering should be in the ‘off’ state. Like this:
- condition: state
entity_id: light.onoff_plug_1
state: 'off'
I do this because I like to check my logs often and I don’t like to see automations firing for no reason and not doing anything, but functionally it doesn’t matter too much.
But it doesn’t like the number 22 for some reason. Do I have to use quotes maybe or other punctuation marks?
You don’t need anything apart from the number there. It’s strange that it doesn’t seem to like that number.
And yeah, I would use the condition to stop the automation firing in the first place if I know for a fact that it shouldn’t be running in those circumstances.
That’s wrong for the value template on a numerical_state trigger. He has the correct syntax. In all other value templates (for the most part) your statement is true.
Also, I’m not sure you are aware, but 22% does not translate to 70 pot value. It translates to 56. So if you wanted to maintain the 70 pot value, you’d want below to be 27.5