Data template for brightness returns strange values

I want to have some lights use a random effect and a certain brightness depending on the chosen effect. The effect part works fine but the brightness level is the one from the previous effect.
This is my script. What is wrong?

      - service: light.turn_on
        data_template:
          entity_id:
            - light.stairs
          effect: >-
           {{ [
           "Chase",
           "Saw",
           "Washing Machine",
           "Pacifica",
           "Solid",
           "Blends"
           ] |random }}
          brightness: >-
           {%- if state_attr("light.stairs", "effect") == 'Chase' %}
           10
           {%- elif state_attr("light.stairs", "effect") == 'Saw' %}
           20
           {%- elif state_attr("light.stairs", "effect") == 'Washing Machine' %}
           30
           {%- elif state_attr("light.stairs", "effect") == 'Pacifica' %}
           40
           {%- elif state_attr("light.stairs", "effect") == 'Solid' %}
           50
           {%- elif state_attr("light.stairs", "effect") == 'Blends' %}
           60
           {% endif -%}

Your script is acting as expected. At the time the script executes the effect is the previous one. The effect doesn’t change until the service runs.

You’d need to set the effect only in a service, then delay for like half a second to make sure that the attribute is changed, then set the brightness in a further service call.

You think like this? Is half a second delay possible?

      - service: light.turn_on
        data_template:
          entity_id:
            - light.stairs
          effect: >-
           {{ [
           "Chase",
           "Saw",
           "Washing Machine",
           "Pacifica",
           "Solid",
           "Blends"
           ] |random }}
      - delay: 0:00:01
      - service: light.turn_on
        data_template:
          entity_id:
            - light.stairs
          brightness: >-
           {%- if state_attr("light.stairs", "effect") == 'Chase' %}
           10
           {%- elif state_attr("light.stairs", "effect") == 'Saw' %}
           20
           {%- elif state_attr("light.stairs", "effect") == 'Washing Machine' %}
           30
           {%- elif state_attr("light.stairs", "effect") == 'Pacifica' %}
           40
           {%- elif state_attr("light.stairs", "effect") == 'Solid' %}
           50
           {%- elif state_attr("light.stairs", "effect") == 'Blends' %}
           60
           {% endif -%}

Yeah, exactly like that. For a half second delay change

- delay: 00:00:01

To

- delay:
    milliseconds: 500

Thanks for helping, it works fine now :slight_smile:

But I have just tried to add an extra attribute, speed. But it returns an error in the logs.
voluptuous.error.MultipleInvalid: extra keys not allowed
Can you tell why?

      - service: light.turn_on
        data_template:
          entity_id:
            - light.stairs
          effect: >-
           {{ [
           "Chase",
           "Saw",
           "Washing Machine",
           "Pacifica",
           "Solid",
           "Blends"
           ] |random }}
      - delay: 0:00:01
      - service: light.turn_on
        data_template:
          entity_id:
            - light.stairs
          brightness: >-
           {%- if state_attr("light.stairs", "effect") == 'Chase' %}
           10
           {%- elif state_attr("light.stairs", "effect") == 'Saw' %}
           20
           {%- elif state_attr("light.stairs", "effect") == 'Washing Machine' %}
           30
           {%- elif state_attr("light.stairs", "effect") == 'Pacifica' %}
           40
           {%- elif state_attr("light.stairs", "effect") == 'Solid' %}
           50
           {%- elif state_attr("light.stairs", "effect") == 'Blends' %}
           60
           {% endif -%}
          speed: >-
           {%- if state_attr("light.stairs", "effect") == 'Chase' %}
           10
           {%- elif state_attr("light.stairs", "effect") == 'Saw' %}
           20
           {%- elif state_attr("light.stairs", "effect") == 'Washing Machine' %}
           30
           {%- elif state_attr("light.stairs", "effect") == 'Pacifica' %}
           40
           {%- elif state_attr("light.stairs", "effect") == 'Solid' %}
           50
           {%- elif state_attr("light.stairs", "effect") == 'Blends' %}
           60
           {% endif -%}
1 Like

Speed isn’t a valid option for the light.turn_on service, all valid options are listed in the table here…

Okay I see
I wonder what could be used instead.
These are the attributes for the light.

brightness: 40
hs_color: 240,100
rgb_color: 0,0,255
xy_color: 0.136,0.04
effect: Pacifica
intensity: 5
palette: Default
playlist: null
preset: null
speed: 3
friendly_name: Stairs
icon: mdi:stairs
supported_features: 55

I suspect that the speed is part of the effect on the device and not configurable externally. (just a guess)

Okay, sad :roll_eyes:
But thanks very much for helping :slight_smile:

1 Like

I solved the issue regarding speed not beeing possible to use with light.turn.on
I used mqtt instead and have this now. In most cases it works but sometimes one of the templates returns a wrong value.
I tried increasing the delay between “effect” and the first template for brightness to 5 seconds, but still same case with wrong values sometimes.
Do you have an idea what could cause this? 5 seconds delay should be more than enough i guess.

      - service: light.turn_on
        data_template:
          entity_id:
            - light.trappe
          effect: >-
           {{ [
           "Chase",
           "Saw",
           "Washing Machine",
           "Pacifica",
           "Solid",
           "Chunchun",
           "Colortwinkles",
           "Blends"
           ] |random }}
      - delay: 0:00:05
      - service: light.turn_on
        data_template:
          entity_id:
            - light.trappe
          brightness: >-
           {%- if state_attr("light.trappe", "effect") == 'Solid' %}
           10
           {%- elif state_attr("light.trappe", "effect") == 'Saw' %}
           15
           {%- elif state_attr("light.trappe", "effect") == 'Washing Machine' %}
           15
           {%- elif state_attr("light.trappe", "effect") == 'Pacifica' %}
           30
           {%- elif state_attr("light.trappe", "effect") == 'Chase' %}
           15
           {%- elif state_attr("light.trappe", "effect") == 'Blends' %}
           40
           {%- elif state_attr("light.trappe", "effect") == 'Chunchun' %}
           30
           {%- elif state_attr("light.trappe", "effect") == 'Colortwinkles' %}
           40
           {% endif -%}
      - delay: 0:00:01
      - service: mqtt.publish #SPEED mqtt bruges kun til speed og intensity for wled da wled.effect først understøttes i HA fra 0.108
        data_template:
          topic: wled/trappe/api
          payload: >-
           {%- if state_attr("light.trappe", "effect") == 'Solid' %}
           SX=10
           {%- elif state_attr("light.trappe", "effect") == 'Saw' %}
           SX=10
           {%- elif state_attr("light.trappe", "effect") == 'Washing Machine' %}
           SX=5
           {%- elif state_attr("light.trappe", "effect") == 'Pacifica' %}
           SX=60
           {%- elif state_attr("light.trappe", "effect") == 'Chase' %}
           SX=50
           {%- elif state_attr("light.trappe", "effect") == 'Blends' %}
           SX=20
           {%- elif state_attr("light.trappe", "effect") == 'Chunchun' %}
           SX=1
           {%- elif state_attr("light.trappe", "effect") == 'Colortwinkles' %}
           SX=40
           {% endif -%}
      - delay: 0:00:01
      - service: mqtt.publish #INTENSITY
        data_template:
          topic: wled/trappe/api
          payload: >-
           {%- if state_attr("light.trappe", "effect") == 'Solid' %}
           IX=10
           {%- elif state_attr("light.trappe", "effect") == 'Saw' %}
           IX=15
           {%- elif state_attr("light.trappe", "effect") == 'Washing Machine' %}
           IX=70
           {%- elif state_attr("light.trappe", "effect") == 'Pacifica' %}
           IX=55
           {%- elif state_attr("light.trappe", "effect") == 'Chase' %}
           IX=175
           {%- elif state_attr("light.trappe", "effect") == 'Blends' %}
           IX=10
           {%- elif state_attr("light.trappe", "effect") == 'Chunchun' %}
           IX=10
           {%- elif state_attr("light.trappe", "effect") == 'Colortwinkles' %}
           IX=20
           {% endif -%}

That looks fine from here, the only thing I can think is that one of the effects is written differently when it’s displayed as an attribute, like maybe ‘Washing Machine’ has a capital M when you call the effect, but when it’s displayed as an attribute it has a lowercase M or something?

Otherwise I’m a bit stumped :man_shrugging:

I looked in the developer tools and alle the attributes have capital letters and looks exactly the same as in the templates… strange!