I want to change brightness of Xiaomi bedside lamp with round Xiaomi wireless switch.
I want do something like that:
When button is pressed then increase brightness continuously. If brightness reaches maximum then change brightness to minimum and continue increase it again.
zmien_jasnosc:
sequence:
- service: light.turn_on
entity_id: light.sypialnia
data_template:
transition: '1'
brightness: >-
{% set n = states.light.sypialnia.attributes.brightness + 45 %}
{% if n > 225 %}
10
{% else %}
{{ n | int }}
{% endif %}
- service: script.zmien_jasnosc2
- wait_template: "{{is_state('script.zmien_jasnosc2', 'off')}}"
zmien_jasnosc2:
sequence:
- service: script.zmien_jasnosc
- wait_template: "{{ is_state('script.zmien_jasnosc', 'off') }}"
Everything is working fine… but only once. No matter how long i have pressed button brightness is increased only by 1 step. Then i must release buton and press it again to increase brightness.
Strange but works with milliseconds: 100.
Unfortunately Xiaomi lamp cannot accept more than few commands per second (I think). Something of “flood protection”.
In logs I’ve got so many entries
"Error rendering data template: UndefinedError: 'mappingproxy object' has no attribute 'brightness'"
and when I have button pressed I see Lamp in offline (unavaliable) state.
To prevent this behaviour I turned on use_music_mode: True in configuration.yaml for platform: yeelight but as you can see - it didn’t helped
I’m looking for an error in a wrong place
Loop is working fine, but infinitely.
When i turned off Lamp loop is still running and trying send commands to device.
This automation should stop the loop after release button but it didn’t work.
- alias: Stop changing brightness
id: '12345678'
trigger:
- platform: state
entity_id: binary_sensor.switch_158d0001dc44c4
to: 'off'
- platform: state
entity_id: binary_sensor.switch_158d0001dc4519
to: 'off'
action:
- service: script.turn_off
entity_id: script.zmien_jasnosc
I thought it’s becouse i stoped only one script. Then I changed action to:
Error executing service <ServiceCall script.turn_off: entity_id=['script.zmien_jasnosc']>
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/homeassistant/core.py", line 1007, in _event_to_service_call
await service_handler.func(service_call)
File "/usr/lib/python3.6/site-packages/homeassistant/components/script.py", line 132, in turn_off_service
in component.async_extract_from_service(service)], loop=hass.loop)
File "/usr/lib/python3.6/asyncio/tasks.py", line 304, in wait
raise ValueError('Set of coroutines/Futures is empty.')
ValueError: Set of coroutines/Futures is empty.
Sorry, didn’t follow the whole thread, but going back to your original implementation (in the first post of this topic), I would suggest:
In the first script, just call the second script, but don’t wait for it to finish.
In the second script, reverse the lines (i.e., wait for the first script to finish, then call it.) Or, if that doesn’t work, make the first step a short delay, then call the first script.
what @pnbruckner suggested isn’t a bad call.
I would delete the stop changing brightness automation though and add in a condition on script.zmien_jasnosc to check if the button is still being held