Setting Brightness From Template Sensor?

Hello!

I’m currently experimenting with Home Assistant, after using openHab for a number of years.

I’m getting an “Expected float for dictionary values @ data[‘brightness_pct’” error, when trying to use some template sensor data to set the brightness of a Phillips Hue lightbulb.

It seems that YAML has a bit of a learning curve, so I’m hoping someone can help me out. It’s probably something simple, but I’ve been wracking my brain over it all evening!

Everything is showing up as expected when I display the entities on the dashboard.

Thanks!

configuration.yaml

sensor:
   - platform: template
     sensors:
       sun_elevation:
         unique_id: "sun_elevation"
         friendly_name: "Sun Elevation"
         unit_of_measurement: "°"
         value_template: "{{state_attr('sun.sun', 'elevation') }}"
       circadian_brightness:
         unique_id: "circadian_brightness"
         friendly_name: "Circadian Brightness"
         unit_of_measurement: "%"
         value_template: >
           {% set max = 0 | float %}
           {% set min = -10 | float %}
           {% set current = state_attr('sun.sun', 'elevation') | float %}
           {% set percent = ((current-min)/(max-min))*100 | float %}
           {% if percent > 100 %}
             100
           {% elif percent < 1 %}
             1
           {% else %}
             {{ percent | float }}
           {% endif %}

Automation

alias: "Hue Brightness "
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.circadian_brightness
condition:
  - condition: state
    entity_id: light.bedroom
    state: "on"
    enabled: true
action:
  - type: turn_on
    device_id: eaca0cd93a71d68436e51c947fe15fba
    entity_id: light.bedroom
    domain: light
    brightness_pct: "{{ states('sensor.circadian_brightness') | float }}"
mode: single

If you are starting out in HA, you should use the current format for template sensors instead of the legacy format, and you should include defaults where necessary

The following is working for me. One thing to check is that your Hue devices support using brightness_pct… not all devices support all the configuration options available in every service.

template:
  - sensor:
      - name: "Sun Elevation"
        unit_of_measurement: "°"
        state_class: measurement
        unique_id: "sun_elevation"
        state: "{{ state_attr('sun.sun', 'elevation') }}"
        availability: "{{ state_attr('sun.sun', 'elevation') is number }}"
      - name: "Circadian Brightness"
        unique_id: "circadian_brightness"
        state_class: measurement
        unit_of_measurement: "%"
        state: >
          {% set max = 0 %}
          {% set min = -10 %}
          {% set current = state_attr('sun.sun', 'elevation') | float(0) %}
          {% set percent = (((current-min)/(max-min)) * 100) | float(0) %}
           {% if percent > 100 %}
             100
           {% elif percent < 1 %}
             1
           {% else %}
             {{ percent }}
           {% endif %}
        availability: "{{ state_attr('sun.sun', 'elevation') is number }}"

FWIW, you can compact your if/then into a single line using sort():

{{ ([1, percent, 100]|sort)[1] }}

alias: "Hue Brightness"
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.circadian_brightness
condition:
  - condition: state
    entity_id: light.bedroom
    state: "on"
action:
  - service: light.turn_on
    target:
      entity_id: light.bedroom
    data:
      brightness_pct: "{{ states('sensor.circadian_brightness') | float(0) }}"
mode: single