Using input_number as brightness_pct for lighting automation

I have an automation that runs my front porch lights. At around sunset, the lights come on at 15%, and turn off at sunrise. If the switch inside is turned on, or motion is detected, the lights go to 100%.

The ‘15%’ value I would like to change to an input_number so I can make it variable, and adjustable from an admin page on the dashboard. (ie it’s halloween, i’ve put in the orange bulbs and I just want it to stay at minimum 50% all night)

Here’s the full automation:

alias: Front Porch Motion Lighting
description: >-
  Automatically turns on the front porch lights between sunset/sunrise and
  toggles full bright for motion or light switch
trigger:
  - platform: sun
    event: sunrise
    offset: '+00:30:00'
    id: sunrise
  - platform: sun
    event: sunset
    offset: '-00:30:00'
  - platform: state
    entity_id: binary_sensor.motion_front_porch
  - platform: state
    entity_id: binary_sensor.motion_front_door
  - platform: state
    entity_id: switch.shellydimmer_frontporch_sw1
condition:
  - condition: or
    conditions:
      - condition: sun
        before: sunrise
        before_offset: '+00:30:01'
      - condition: sun
        after: sunset
        after_offset: '-00:30:00'
action:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: state
                entity_id: binary_sensor.motion_front_door
                state: 'on'
              - condition: state
                entity_id: binary_sensor.motion_front_porch
                state: 'on'
              - condition: state
                entity_id: switch.shellydimmer_frontporch_sw1
                state: 'on'
        sequence:
          - type: turn_on
            device_id: 55f8402b5324074cdcf2cb13f9aa2b45
            entity_id: light.shellydimmer_frontporch
            domain: light
            brightness_pct: 100
      - conditions:
          - condition: trigger
            id: sunrise
          - condition: state
            entity_id: switch.shellydimmer_frontporch_sw1
            state: 'off'
        sequence:
          - type: turn_off
            device_id: 55f8402b5324074cdcf2cb13f9aa2b45
            entity_id: light.shellydimmer_frontporch
            domain: light
      - conditions:
          - condition: and
            conditions:
              - condition: state
                entity_id: binary_sensor.motion_front_door
                state: 'off'
              - condition: state
                entity_id: binary_sensor.motion_front_porch
                state: 'off'
              - condition: state
                entity_id: switch.shellydimmer_frontporch_sw1
                state: 'off'
        sequence:
          - type: turn_on
            device_id: 55f8402b5324074cdcf2cb13f9aa2b45
            entity_id: light.shellydimmer_frontporch
            domain: light
            brightness_pct: 15
    default:
      - type: turn_off
        device_id: 55f8402b5324074cdcf2cb13f9aa2b45
        entity_id: light.shellydimmer_frontporch
        domain: light
mode: single

Here’s the part I want to change:

        sequence:
          - type: turn_on
            device_id: 55f8402b5324074cdcf2cb13f9aa2b45
            entity_id: light.shellydimmer_frontporch
            domain: light
            brightness_pct: 15

To

        sequence:
          - type: turn_on
            device_id: 55f8402b5324074cdcf2cb13f9aa2b45
            entity_id: light.shellydimmer_frontporch
            domain: light
            brightness_pct: {{ states('input_number.brightness_night_front_porch') }}

Trying to save with that change results in a “Message malformed: expected float for dictionary value @ data[‘brightness_pct’]”

I have tried the following, with no change in error.

            brightness_pct: {{ states('input_number.brightness_night_front_porch') | Float }}
            brightness_pct: {{ states('input_number.brightness_night_front_porch') | Int }}
            brightness_pct: {{ (states('input_number.brightness_night_front_porch') | Float ) }}
            brightness_pct: {{ states.input_number.brightness_night_front_porch.state }}
            brightness_pct: {{ states.input_number.brightness_night_front_porch.state |int }}
            brightness_pct: {{ states.input_number.brightness_night_front_porch.state |float }}

What am I missing?

Edit: I did check using the template tab on the Developer’s Tools, and it returns a “50.0” which is what the entity is currently set to, along with the following attributes

initial: null
editable: true
min: 0
max: 100
step: 1
mode: slider
unit_of_measurement: '%'
friendly_name: Brightness_Night_Front_Porch
icon: mdi:lightbulb-outline
1 Like

Here is the code I am using - ignore that I am using a trigger for the entity_id.

- service: light.turn_on
  data_template:
    entity_id: '{{ trigger.entity_id }}'
    brightness_pct: '{{ states(''input_number.livingroom_light'')|int }}'
1 Like

I tried doubling up some single-quotes like yours and no change. Also tried some more variations. Still no luck.

If I delete the template and just put “50” there (no quotes) it goes back to working so it doesn’t seem like any other issues from the yaml bleeding over…

It’s not just about the quotes, that’s just the tip of the problem.
It’s about the fact that the brightness_pct needs to be under data_template if you are using a template for the value. (That’s why you were getting the dictionary error)

Ahhhh ok. I was simply trying to modify what was provided from the visual editor.
Thank you. I have started from scratch for the action, using the light.turn_on service and it appears to be working under data template.

service: light.turn_on
data_template:
  entity_id: light.shellydimmer_frontporch
  brightness_pct: '{{ states(''input_number.brightness_night_front_porch'') |int }}'

For fun i created a new action using the visual editor and it created one with a data field, so it’s much easier to adapt going forward. I’d originally created this automation many versions ago.

Thank you for the help and guidance!

1 Like

No problem. Please consider marking the post as solved.
There probably is a way of doing it under the newer way - I know there is talk of phasing out data_template and having stuff just work under data, and this has happened in some places, but clearly it hasn’t happened everywhere yet.

oh that would wildly simplify the learning curve.
I kind of interpretted the ‘{{’ and "}}’ to be similar to other languages where it just auto inserts the output of whatever is between the curlies. I would definitely prefer that.

I already marked it as solved (i believe i did anyway…) so should be good.

Thanks again!

1 Like

That would probably be the case if it was more simple stuff that it was dealing with, but I assume that Home Assistant is running the code every time it evaluates that piece of the code, not just once when it reads the YAML. So it needs to be “told” that this piece of code needs to be evaluated dynamically each time it is needed, rather than this is a value that is static.