Hello,
I have Xiaomi wireless buttons working with LIFX WiFi lights.
A single click of the button will toggle the light on or off, the opposite of the current state - and the bulb always remembers the last set brightness level.
What I am having trouble with is configuring a double-click of the button to toggle between 50% and 100% brightness for the light.
I can configure a double-click to set a specific brightness - however sometimes we want it full brightness, and other times half brightness.
I have been Googleing, reading, and trying various data_template settings but so far none successfully.
action:
- service: light.turn_on
data_template:
entity_id: light.bedroom
brightness: >
{% if states.light.bedroom.attributes.brightness > 240 %}
130
{% elif states.light.bedroom.attributes.brightness < 150 %}
250
{% endif %}
When I double-click the button, I would like it to check the current brightness level, and if its over 240 (bright), change the brightness to 130 (dim), and if its below 150 (dim), change the brightness to 250 (bright).
The config above Validates successfully on HASS.IO 0.65.4
However when I double-click I get the following error in the logs:
Invalid service data for light.turn_on: expected int for dictionary value @ data[‘brightness’]. Got ‘’
HomeAssistant reports these states for the bulb:
min_mireds: 111
max_mireds: 400
brightness: 179
color_temp: 285
rgb_color: 178,178,178
effect_list: lifx_effect_colorloop,lifx_effect_pulse,lifx_effect_stop
Any help is appreciated.
The only thing I can see wrong is the spacing on the second line. Should be:
action:
- service: light.turn_on
data_template:
entity_id: light.bedroom
brightness: >
{% if states.light.bedroom.attributes.brightness > 240 %}
130
{% elif states.light.bedroom.attributes.brightness < 150 %}
250
{% endif %}
You could also try something like this, it’s how I’ve set up my Lifx lights:
action:
- service: light.turn_on
data_template:
entity_id: light.bedroom
brightness: >
{% if states.light.bedroom.attributes.brightness < 150 %}
250
{% elif states.light.bedroom.attributes.brightness < 255 %}
130
{% endif %}
1 Like
Thank you so much - ah I cant believe how close I was - just 1 indentation out of line…
Do you find it practical and useful to be able to toggle the light on/off with 1 action, and toggle brightness up/down with another ?
My lights are all automated based on time of day/occupancy, so I hardly have to do anything with that, and even if I do, I just use Alexa for that. I have a few scripts (dimming lights when watching TV, dimming lights when not home, etc), that run as part of different automations.
I have been testing this automation - it works great when the lights are already on (regardless of brightness - it toggles to the opposite of bright or dim).
However, if the light is off, and I double-click the switch, nothing happens and there is an error in the logs as HA cannot read the brightness state when the light is off.
I have fixed this by adding a default value to the IF query.
I also realised I do not need to do a 2nd brightness check - as the first check identifies if the light was in bright or dim mode - so changed the second part from ELIF to ELSE.
action:
- service: light.turn_on
data_template:
entity_id: light.bedroom
transition: 2
brightness: >
{% if states.light.bedroom.attributes.brightness|default(255) < 250 %}
255
{% else %}
140
{% endif %}
I chose the highest default number (255), so the double-click automation rule will default to turning the light on to half brightness (the opposite of the high number) instead of defaulting to full brightness which happens if I set the default to 0 (or anything below 250).
We have a newborn baby sleeping in our bedroom so full brightness in the middle of the night is not a good thing. Now, if the light is off and we are uncertain if the bulb was last set to full brightness, we can double-tap and know it will come on at half brightness.
2 Likes
Thanks for the inspiration, I tweaked your code for my bedroom light that:
- if the light is on or dim
1a. turn off
- otherwise (ie off)
2a. dim to 80
action:
- service: light.turn_on
data_template:
entity_id: light.main_bed
brightness: >
{% if states.light.main_bed.attributes.brightness|default(0) > 0 %}
0
{% else %}
80
{% endif %}
I’ve simplified the data template further:
action:
service: light.turn_on
entity_id: light.bedroom
data_template:
brightness: '{{ 254 if states.light.bedroom.attributes.brightness|default(254) < 254 else 140 }}'
1 Like