Lights Templating Issue

I am having an issue where the script works fine for one automation, but fails on another with:

expected float for dictionary value @ data['brightness_pct']

I tried adding float to the below lights_on_brightness script, which just turned the lights to 0 pct, but I don’t know why it would work fine for one but fail for another when I’m passing the same values in the data_template.

Here’s the lights script

lights_on_brightness:
  alias: Lights on brightness script
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: "{{ entity_id }}"
        brightness_pct: >
          {% if brightness_pct is defined %}
            {{ brightness_pct }}
          {% else %}
            100
          {% endif %}
        transition: >
          {% if transition is defined %}
            {{ transition }}
          {% else %}
            3
          {% endif %}

Here’s a working automation using it.

- alias: Extra Button Light
  initial_state: on
  trigger:
    - platform: event
      event_data:
        command: button_single
        device_ieee: '28:6d:97:00:01:05:b6:2c'
      event_type: zha_event
    - platform: event
      event_data:
        command: button_double
        device_ieee: '28:6d:97:00:01:05:b6:2c'
      event_type: zha_event
    - platform: event
      event_data:
        command: button_hold
        device_ieee: '28:6d:97:00:01:05:b6:2c'
      event_type: zha_event
  action:
    data_template:
      entity_id: light.extra_light
      brightness_pct: >-
        {% if (trigger.event.data.command) == "button_single" %}
          70
        {% elif (trigger.event.data.command) == "button_double" %}
          10
        {% endif %}
    service_template: >-
      {% if (trigger.event.data.command) == "button_single" %}
        script.lights_on_brightness
      {% elif (trigger.event.data.command) == "button_double" %}
        script.lights_on_brightness
      {% else %}
        script.lights_off
      {% endif %}

But here’s the automation I’m getting the error on and need help with

 alias: Plex Media Lights
  initial_state: on
  trigger:
    - platform: state
      entity_id: media_player.roku_ultra_2
    - platform: state
      entity_id: media_player.roku_ultra_3

  action:
    - service: script.lights_on_brightness
      data_template:
        entity_id: group.house_lights
        transition: 3
        brightness_pct: >-
          {% if trigger.state == "playing" %}
            10
          {% elif trigger.state == "paused" %}
            30
          {% elif trigger.state == "idle" and trigger.entity_id != "media_player.roku_ultra_3" %}
            50
          {% endif %}

What happens if your media player is in none of these states (e.g. off)?

        brightness_pct: >-
          {% if trigger.state == "playing" %}
            10
          {% elif trigger.state == "paused" %}
            30
          {% elif trigger.state == "idle" and trigger.entity_id != "media_player.roku_ultra_3" %}
            50
          {% endif %}

Answer: it gets no value.

As you are triggering on any change of state of the media player this is quite possible. Add an else statement to catch all other states (off, unknown, unavailable, etc…).

Thanks tom_I, I just adjusted the triggers to only answer on the states I needed to test but I am still getting the same error.

trigger:
    - platform: state
      entity_id: media_player.roku_ultra_2
      to: 'idle'
    - platform: state
      entity_id: media_player.roku_ultra_2
      to: 'playing'
    - platform: state
      entity_id: media_player.roku_ultra_3
      to: 'playing'
    - platform: state
      entity_id: media_player.roku_ultra_2
      to: 'paused'
    - platform: state
      entity_id: media_player.roku_ultra_3
      to: 'paused'

I added in your else and it triggered every time, whether the roku was playing or not. I’m guessing there’s something wrong with my trigger.state

        brightness_pct: >-
          {% if trigger.state == "playing" %}
            10
          {% elif trigger.state == "paused" %}
            30
          {% elif trigger.state == "idle" %}
            50
          {% else %}
            50
          {% endif %}

Well that’s odd. I can’t see any reason why one would work but not the other.

Well trigger.state isn’t valid, I tried trigger.to_state but getting same issue.

trigger.to_state.state

Nailed it! Appreciate your eyes on this.

1 Like