Script to change brightness Xiaomi Bedside Lamp

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.

I have configuration.yaml:

light:
  - platform: yeelight
    devices:
      192.168.1.15:
        name: Bedroom
        transition: 1
        save_on_change: False
        use_music_mode: True

automations:

- alias: Change brightness
  id: '12345677'
  trigger:
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001dc44c4
      click_type: long_click_press
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001dc4519
      click_type: long_click_press
  action:
   - service: script.turn_on
     entity_id: script.zmien_jasnosc
    
- 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

and scripts:

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.

switch the last 2 lines round on each script. the scripts are calling each other before waiting for them to end

Nothing changes. Brightness increases only once. In logs i’ve got an error now:

Script script.zmien_jasnosc already running.

Im using somthing simlar to turn off my TV because I get in the way of my IR blaster but im using delays maybe try;

- wait_template: "{{is_state('script.zmien_jasnosc2', 'off')}}"
- delay:
    milliseconds: 1
- service: script.zmien_jasnosc2

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

Not ideal but maybe make the delay 1 or 2 seconds and do bigger jumps on the brightness?

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:

   - service: script.turn_off
     entity_id: script.zmien_jasnosc
   - service: script.turn_off
     entity_id: script.zmien_jasnosc2

but afer that i’ve got this:

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.

and brightness not changes even once.

Sorry, didn’t follow the whole thread, but going back to your original implementation (in the first post of this topic), I would suggest:

  1. In the first script, just call the second script, but don’t wait for it to finish.
  2. 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.
  3. In the second automation, do:
  action:
    service: script.turn_off
    entity_id: script.zmien_jasnosc, script.jasnosc2
1 Like

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

1 Like

Finally! I have to fully restart Hass.io. After restart all errors disappeared, and automations and scripts executing fine.
Thank you all for help.

No probs. Perhaps you could post your final config so if anyone else tripps over this post it could help them out

OK. This is my final config:

configuration.yaml

light:
  - platform: yeelight
    devices:
      192.168.1.15:
        name: Sypialnia
        transition: 1
        save_on_change: False
        use_music_mode: True

automations.yaml

- alias: Change brightness
  id: '12345677'
  trigger:
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001dc44c4
      click_type: long_click_press
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001dc4519
      click_type: long_click_press
  action:
   - service: script.turn_on
     entity_id: script.zmien_jasnosc
    
- 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
   - service: script.turn_off
     entity_id: script.zmien_jasnosc2

scripts.yaml

zmien_jasnosc:
    sequence:
    - service: light.turn_on
      entity_id: light.sypialnia
      data_template:
       transition: '1'
       brightness: >-
        {% set n = states.light.sypialnia.attributes.brightness + 15 %}
        {% if n > 235 %}
          10
        {% else %}
         {{ n | int }}
        {% endif %}
    - wait_template: "{{is_state('script.zmien_jasnosc2', 'off')}}"
    - delay:
       milliseconds: 100
    - service: script.zmien_jasnosc2

zmien_jasnosc2:
    sequence:
    - wait_template: "{{ is_state('script.zmien_jasnosc', 'off') }}"
    - service: script.zmien_jasnosc
1 Like