Hi there,
What is the best way to achieve following in automation.yaml on motion sensor trigger to turn on the hue lights:
if time between 10pm and 6am then brightness 10, else 255.
set color of the lights randomly with 90% chance to get normal white and 10% chance it gets purple.
I spent a few hours and tried some examples from this forum but none of them worked for me. One of the issues was that when doing 'check config in hassbian i got error complaining about ‘%’ symbol in automation.yaml where i used if then statement.
Another one was that when i used color_name and random operation that particular automation just stopped working.
Automation works if i just set up specific brightness and color. It is when i try to build above rules it is failing.
I wonder if someone can help with best practice and syntax to achieve this?
Thanks a lot for this @lolouk44
It works fine with brightness but when i add color_name line it stops working. The rest of automation works fine but lights don’t turn on any more. Do you know what can be a problem here?
Like for example do i need to declare random somewhere in configuration.yaml or anything like this?
Just checked this again and can’t see what’s wrong.what happens if you replace the colour name template either just white or purple?
Also try this though I’m not sure it’ll make much difference color_name: '{%- if (range(0,11) | random) <= 9 -%}white{%- else -%}purple{%- endif -%}'
That won’t work because rgb_color requires a list of three numbers, and using a template like that will always return one string (even if it looks like a list of three numbers.)
The suggestions from @lolouk44 and @datamonkey should work. If they’re not, then it could be there’s a typo in your YAML code. It’s always good to post what you have so syntax/spacing errors can be found (which are common.) Also, can you elaborate on “it doesn’t work”? E.g., are there any error messages?
Hi @lolouk44, @pnbruckner
Looks like color_name doesnt work for me at all.
Here is a piece from automation.yaml:
- id: '1536773835545'
alias: Toilet lights on
trigger:
- entity_id: binary_sensor.motion_sensor_158d00023e81a4
from: 'off'
platform: state
to: 'on'
condition:
- below: '50'
condition: numeric_state
entity_id: sensor.illumination_158d00023e81a4
action:
data_template:
entity_id: light.toilet_lamp
brightness: '{% if now().hour > 22 or now().hour < 6%} 1 {% else %} 255 {% endif %}'
color_name: green
color_temp: 600
service: light.turn_on
It doesn’t throw any error when i do config_check but it doesn’t work either.
When i comment color_name: green it starts working.
I tried to move color_name: green from data_template to data and using single and double quotes color_name: 'green' but this didn’t work either.
You can’t use these two together. In fact you can only use one color type in a service call, period. Remove the color_temp and it will work (unless your particular light doesn’t support RGB color.) And there should have been an error since the service schema would have rejected the call as-is.