Not returning integers in data {} automations

I have the following and I can’t get the value to return integer (this is a simple test I want to put an if statement there):

  "brightness": "{{ states.light.t_room.attributes.brightness }}",
  "entity_id": "light.t_room"

I just get the following error, I’m editing the automation via the web front end and can’t remove the quotes (wondering if it’s a bug in the UI stopping me save it, or use fault:

Invalid service data for light.turn_on: expected int for dictionary value @ data['brightness']. Got '{{ states.light.t_room.attributes.brightness }}'

Add a cast filter:

"brightness": "{{ states.light.t_room.attributes.brightness | int }}"

Unfortunately that doesn’t work - still get the same error

Please show the entire action configuration. My assumption is an inproper use of data_template or service_template.

{ "brightness": "{{ states.light.t_room.attributes.brightness | int }}" "entity_id": "light.t_room" }

Using the web front end. Works fine if I replace the {{ states.light.t_room.attributes.brightness | int }} with 255 in quotes

Context please, do you use this code sequence in a sensor, an automation or a script? Clearly “brightness” is expecting a integer value, not a string. The quotes around the template convert the result into a string, that’s what causing the error.

1 Like

Do you mean in Home-Assistant’s service tool at path http://your-ha-host/dev-service? This tool does not support dynamic data computing at all.

“brightness”: “{{ states.light.t_room.attributes.brightness }}”,
“entity_id”: “light.t_room”

if you use {{ }} in any yaml configuration, it needs to be in an area that accepts a data_template of some sort. It looks like you are trying to use it in a service. If that’s the case, you need to use a data_template for said service:

- service: light.turn_on
  entity_id: light.t_room
    brightness: "{{ states.light.t_room.attributes.brightness | int }}"

But to me, that service doesn’t make sense because you are pulling the devices brightness and placing it into itself… which doesn’t make sense as devices inherently turn on with the last brightness level.

From a ‘whole picture’ standpoint, what are you trying to do?

I’m actually trying to put an if statement there to toggle the brightness, if it’s dimmed (i.e. 1) then set it to 255 otherwise set to 1. I have a binary switch that triggers this automation and does a number of things. This is the last step.

So this service is in the automation under the action? Can you post your yaml that is having the issues?