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