Xiaomi Cube -> Service Template

I’m trying to use the rotate cube action to turn a led strip on if turning to the right and off if turning to the left. To do that I thought I’d use a service_template so I create the following:

  • alias: Xiaomi Cube - Toggle Led Strip [Rotate]
    initial_state: true
    hide_entity: true
    trigger:
    • platform: event
      event_type: cube_action
      event_data:
      entity_id: binary_sensor.cube_158d0001100c4e
      action_type: rotate
      action:
    • service_template: >
    • service_template: >
      {%if trigger.event.data.action_value | float > 0 %}
      {{ light.turn_on | int + 50 }}
      {% else %}
      {{ light.turn_off }}
      {% endif %}
      entity_id: light.led_strip

This is not working :frowning: I get this error in syslog:
[homeassistant.helpers.service] Error rendering service name template: UndefinedError: 'light' is undefined

What am I doing wrong?

Hey Mozart give this a try:

alias: Xiaomi Cube - Toggle Led Strip [Rotate]
initial_state: 'on'
trigger:
  platform: event
  event_type: cube_action
  event_data:
    entity_id: binary_sensor.cube_158d0001100c4e
    action_type: rotate
action:
  - service: light.turn_on
    data_template:
      entity_id: light.led_strip
      brightness: >-
        {%if trigger.event.data.action_value | float > 0 %}
        {{  states.light.led_strip.attributes.brightness | int + 255 }}
        {% else %}
        {{  states.light.led_strip.attributes.brightness | int - 255 }}
        {% endif %}

I think this will work for you and if you change int + 255 and int - 255 to something lower like int + 50 and int - 50 this will brighten and dim your led strip, should work let me know.

You’ve put brackets round the light services, they need to be removed.

So the if lines have brackets, then the service doesn’t, what you’re saying is if the stuff inside the bracket is true, do the service outside the bracket, if it’s false skip to the next bracket.

At the moment the next bracket doesn’t contain an if it contains a light, hence the error.

Also, the brightness can’t be defined how you’ve defined it, and you’ve declared service_template: twice.

Thanks for the tips. I’ll see if I can get it working…

I got it working. This is what I made of it:

- id: ledstrip_toggle
  alias: Xiaomi Cube - Toggle Led Strip [Rotate]
  initial_state: true
  hide_entity: true
  trigger:
  - platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_158d0001100c4e
      action_type: rotate
  action:         
  - service_template: >
      {% if trigger.event.data.action_value | float > 0 %}
      light.turn_on
      {% else %}
      light.turn_off
      {% endif %}
    entity_id: light.led_strip

On to the next challenge: changing to a different effect when shaking the cube :slight_smile:

1 Like