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:
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?
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.
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!
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.
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.
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.
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.