Using a variable in scripts with brightness_pct

I wrote several simple scripts to turn on the lights in my house and I’m trying to get the dimmer to change depending on the time of day. Here’s one of the most basic scripts:

alias: Office Light
sequence:

  • type: turn_on
    device_id: 5c8368a214e8941e9041b5496866ce19
    entity_id: light.boob_light
    domain: light
    brightness_pct: ‘100’
    mode: single
    icon: mdi:currency-usd

Now here’s my evil, nasty little ?!#?!>?!? of a problem: every time I try to replace the 100 with:

‘{{ (states(“var.light_pct”) | int) }}’

it fails. Every time. I get the following error message:

Message malformed: expected float for dictionary value @ data[‘brightness_pct’]

The states command works – I can get notify to send a push notification with the exact number, no problem. So I haven’t the foggiest flippin’ idea why I can’t get past this.

Anyone out there think they might be able to help me?

Oh, and before anyone goes for the obvious – yes, the first thing I tried to do was replace int with float. Still doesn’t work.

This is what I have:

          - service: light.turn_on
            data_template:
              entity_id: '{{ light_target }}'
              transition: 30
              brightness_pct: '{{ brightness_target|int }}'

1 Like

Because a Device Action’s brightness_pct option doesn’t support templates. That’s what the error message indicates; it only accepts a float value (i.e. a number).

Because you have been doing the software equivalent of hammering a square peg into a round hole. You need to use a round peg, like a service call. :slightly_smiling_face:

Use a service call, like this:

- service: light.turn_on
  target:
    entity_id: light.boob_light
  data:
    brightness_pct: '{{ states("var.light_pct") }}'

As long as the value of var.light_pct is a numeric string, Home Assistant’s native typing will convert it to a number so there’s no need to use an int filter in the template.

Well…now that I have finally recovered from banging my head against the wall so many times…I switched over to a service call and it, naturally, worked like a charm.

Thanks for helping this noob get her pegs straight!

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic is resolved. This helps other users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.

Done. Thank you again!

Any chance I could trouble you for one last bit of help?

Here’s my code:

alias: TEST SCRIPT
sequence:

  • service: notify.notify
    data:
    message: ‘{{ states(“var.light_pct”) }}’
  • service: light.turn_on
    target:
    area_id: office
    device_id: 5c8368a214e8941e9041b5496866ce19
    data:
    brightness_pct: ‘{{ states(“var.light_pct”) }}’
    mode: single
    icon: mdi:currency-usd

(ignore the notify – that’s for debugging and I’ll delete it once I get past my latest headache.)

and here’s where I’m trying to determine var.light_pct in my config file:

var:
light_pct:
force_update: true
restore: false
friendly_name: “light pct”
initial_value: >-
{% if now() > today_at(“22:00”) and now() < today_at (“06:00”) %}
25
{% elif (now() > today_at(“06:00”) and now() < today_at (“09:00”)) or (now() > today_at(“17:00”) and now() < today_at (“22:00”)) %}
50
{% else %}
100
{% endif %}

My problem is an extension of the previous one: I’m getting the schedule to return the right numeric value, but it’s not recognizing the number as a numeric string, and when I add “| int” to the script call, it returns back 0. What am I missing?

Go to Developer Tools > Template and copy-paste the following template:

{{ states("var.light_pct") }}

It should report whatever is the current value of var.light. If the value it reports is numeric (regardless if its type is string or number), and between 0 and 100, then that’s sufficient for use by the brightness_pct option.

Try this version of the script:

alias: TEST SCRIPT
sequence:
  - service: notify.notify
    data:
      message: '{{ states("var.light_pct") }}'
  - service: light.turn_on
    target:
      area_id: office
    data:
      brightness_pct: '{{ states("var.light_pct") }}'
mode: single
icon: mdi:currency-usd

If it fails to work, check the script’s trace which will reveal exactly what happened when the script was executed.

The template is returning the full if-then statement?! When I put the if-then statement into the template, though, that returns the number correctly.

And the trace – that also returned the full if-then statement…

I don’t know what you mean when you say “full if-then statement”. There is no “if-then” decision-making performed by either of the two templates I posted above.

This is what the template tool and the trace returns when I look for that brightness number:

{% if now() > today_at(“22:00”) and now() < today_at (“06:00”) %}
25
{% elif (now() > today_at(“06:00”) and now() < today_at (“09:00”)) or (now() > today_at(“17:00”) and now() < today_at (“22:00”)) %}
50
{% else %}
100
{% endif %}

I didn’t make any suggestions about the configuration of var.light_pct, only for the script because you posted it as unformatted YAML thereby making it challenging to interpret.

Home Assistant does not have a native var domain so I believe you are using a custom component that introduces a form of global variables. I don’t use that custom component so can’t comment on it other than its template seems to function properly.