Providing int but getting error about not providing int

Hi everybody,

I am trying to implement a Xiaomi Cube in my office workflow. If turned, it’s supposed to change the current brightness of a lamp.

service: light.turn_on
target:
  entity_id: light.arbeitszimmer_licht_gruen
data:
  brightness: >
    {{ state_attr("light.arbeitszimmer_licht_gruen", "brightness") | int - 10 }}

I checked this via templates and get this output

service: light.turn_on
data:
  entity_id: light.arbeitszimmer_licht_gruen
  brightness: >
    110

When I run the code posted first as a service, I get Fehler beim Aufrufen des Diensts light/turn_on. expected int for dictionary value @ data['brightness']. However, this is an int, isn’t it?

When I use the code from above with brightness: 100 (instead of the template), it works fine. Using data_template instead of data does not work.

I was hoping to gradually lower the brightness whenever the cube sends action: rotate_left (and the other way round).

Thank you for your help :slight_smile:

Is light.arbeitszimmer_licht_gruen off?

When a light is off it has no brightness attribute and the template’s result will be -10.

1 Like

No, it is currently on. I get the correct value in templates, so the state is no -10 (but currently 120).

Interesting. The template looks good (and brightness accepts a template) so I fail to see why it results in an error message.

You are using the format shown in the first example? The one with target?

1 Like

I tried all sorts of variants of this. But I thought target should work as it is provided be the service UI (when I create this via UI and then switch to yaml).

Are you testing this using Developer Tools > Services? Unless something changed in the latest release, that has never supported templates.

1 Like

Oh damn! Yeah, that is how I was testing it. I will try writing the yaml as I believe it should work and save it. Perhaps that will work, will report later.

That’s why it works when the value is an integer but not when it’s a template.

Put it in a script and it will work.

1 Like

Why not make it simpler by just using the brightness_step parameter instead of brightness with a template? See light.turn_on service. E.g.,

service: light.turn_on
target:
  entity_id: light.arbeitszimmer_licht_gruen
data:
  brightness_step: -10

I believe this also correctly handles the case when the step would potentially cause the brightness to go negative (which your template did not.)

1 Like

Thank you both @123 @pnbruckner :pray:

It works when put in an automation (well, turning up the brightness works perfectly; turning it down will always turn the lamp off alltogether, regardless of the current brightness).

The brightness_step parameter works so much better than my approach :slight_smile:

Don’t forget that the root cause of the original problem was the use of templates in Developer Tools > Services. Should you ever need to template brightness_step, don’t forget you can’t test it in Developer Tools > Services.

Yes, I will definitely make sure to remember this next time :slight_smile: I should have figured because !secret doesn’t work there, either (at least the last time I tried it), but I did so much in Home Assistant today that it must have slipped my mind… I got a README.me in my Home Assistant root folder and will link this topic to it!