Template with simple subtraction not working, but working with addition

Hello, I am having trouble finding an error inside an automation. I am using an IKEA remote to control a cwww light and I want to use the left and right buttons to change the temperature. I am using a blueprint and adding the automation manually. The weird thing is that the right button works and increases the temperature, while the left one doesn’t. If I check the trace I see that the service is called but the temperature doesn’t change, while with the right one it does, even though it doesn’t increment by 10, which is also weird.
This is the code in the automation:

alias: ZHA automazione telecomando ikea
description: ""
use_blueprint:
  path: Malte/zha-ikea-four-button-remote-styrbar-for-lights-e2001-e2002.yaml
  input:
    remote: 54a4f5a4cf9c85e0c989f555e18e4999
    light:
      entity_id: light.led_mansarda
    button_left_long:
      - repeat:
          count: 300
          sequence:
            - service: light.turn_on
              data:
                transition: 0.1
                color_temp_kelvin: >-
                  {{ state_attr('light.led_mansarda','color_temp_kelvin')|int -
                  10 }}
              target:
                entity_id: light.led_mansarda
            - delay: 0.1
    button_right_long:
      - repeat:
          count: 300
          sequence:
            - service: light.turn_on
              data:
                transition: 0.1
                color_temp_kelvin: >-
                  {{ state_attr('light.led_mansarda','color_temp_kelvin')|int +
                  10 }}
              target:
                entity_id: light.led_mansarda
            - delay: 0.1

while this is what I get in the traces:

Iteration 1
Executed: 22 novembre 2022 15:36:55
Result:
params:
  domain: light
  service: turn_on
  service_data:
    transition: 0.1
    color_temp_kelvin: 4494
    entity_id:
      - light.led_mansarda
  target:
    entity_id:
      - light.led_mansarda
running_script: false
limit: 10
Iteration 2
Executed: 22 novembre 2022 15:36:56
Result:
params:
  domain: light
  service: turn_on
  service_data:
    transition: 0.1
    color_temp_kelvin: 4494
    entity_id:
      - light.led_mansarda
  target:
    entity_id:
      - light.led_mansarda
running_script: false
limit: 10
Iteration 3
Executed: 22 novembre 2022 15:36:56
Result:
params:
  domain: light
  service: turn_on
  service_data:
    transition: 0.1
    color_temp_kelvin: 4494
    entity_id:
      - light.led_mansarda
  target:
    entity_id:
      - light.led_mansarda
running_script: false
limit: 10

If instead I use the right arrow this is what I get:

Iteration 1
Executed: 22 novembre 2022 15:41:01
Result:
params:
  domain: light
  service: turn_on
  service_data:
    transition: 0.1
    color_temp_kelvin: 4514
    entity_id:
      - light.led_mansarda
  target:
    entity_id:
      - light.led_mansarda
running_script: false
limit: 10
Iteration 2
Executed: 22 novembre 2022 15:41:01
Result:
params:
  domain: light
  service: turn_on
  service_data:
    transition: 0.1
    color_temp_kelvin: 4534
    entity_id:
      - light.led_mansarda
  target:
    entity_id:
      - light.led_mansarda
running_script: false
limit: 10
Iteration 3
Executed: 22 novembre 2022 15:41:01
Result:
params:
  domain: light
  service: turn_on
  service_data:
    transition: 0.1
    color_temp_kelvin: 4555
    entity_id:
      - light.led_mansarda
  target:
    entity_id:
      - light.led_mansarda
running_script: false
limit: 10

The color temperature doesn’t increase by 10 but it works and the temperature increases.

Please, don’t share images of code, share code as code

if you’re going from an off state, that attribute won’t exist and that template will error. If you’re going from an on state, that attribute needs to exist which depends on your light functionality.

Just looking at the lights I own that are color changing and have white levels, that attribute doesn’t exist when on or off.

When off, no color attributes exist.

When on, with a color level, “color_mode”, “brightness”, “hs_color”, “rgb_color”, and “xy_color” exist.

When on, with a temperature, “color_temp_kelvin”, “color_temp”, “hs_color”, “rgb_color”, and “xy_color” exist.

i.e. your tempalte will need to cover those scenarios and handle them appropriately instead of blindly grabbing the attribute.

Sorry, I’ve edited the post!

Thanks for the reply! However the way the Ikea remote works, it always sends an “ON” command before the long press event so it shouldn’t be needed to check that the light is on. Besides, the weird thing is that it works if I use “+” instead of “-”, that is if I add 10 to the temperature instead of subtracting it.

Still doesn’t guarantee that the attribute exists as there are multiple color modes and each mode will change the available attributes.

I understand, I thought it wasn’t necessary since the light I am using only works with color temperature. Anyway I have added the check for the existence of the attribute and I have also managed to solve my issue. Apparently the problem was with the use of Kelvin and some internal conversion HA probably uses. By switching to mireds everything works. Also I have tried by using a bigger Kelvin increment (50 instead of 10) and it also works. This is my final code:

alias: ZHA automazione telecomando ikea
description: ""
use_blueprint:
  path: Malte/zha-ikea-four-button-remote-styrbar-for-lights-e2001-e2002.yaml
  input:
    remote: 54a4f5a4cf9c85e0c989f555e18e4999
    light:
      entity_id: light.led_mansarda
    button_left_long:
      - repeat:
          count: 300
          sequence:
            - service: light.turn_on
              data:
                transition: 0.1
                color_temp: >-
                  {% if state_attr("light.led_mansarda", "color_temp") != None
                  %}
                    {% if state_attr("light.led_mansarda", "color_temp") <= 315 %}
                      {{ state_attr('light.led_mansarda','color_temp')|int + 5}}
                    {% else %}
                      {{ 320 }}
                    {% endif %}
                  {% else %}
                      {{ 320 }}
                  {% endif %}
              target:
                entity_id: light.led_mansarda
            - delay:
                hours: 0
                minutes: 0
                seconds: 0
                milliseconds: 200
    button_right_long:
      - repeat:
          count: 300
          sequence:
            - service: light.turn_on
              data:
                transition: 0.1
                color_temp: >-
                  {% if state_attr("light.led_mansarda", "color_temp") != None
                  %}
                    {% if state_attr("light.led_mansarda", "color_temp") >= 165 %}
                      {{ state_attr('light.led_mansarda','color_temp')|int - 5}}
                    {% else %}
                      {{ 160 }}
                    {% endif %}
                  {% else %}
                      {{ 160 }}
                  {% endif %}
              target:
                entity_id: light.led_mansarda
            - delay:
                hours: 0
                minutes: 0
                seconds: 0
                milliseconds: 200
    button_left_short:
      - service: light.turn_on
        data:
          transition: 0.1
          color_temp: >-
            {% if state_attr("light.led_mansarda", "color_temp") != None %}
              {% if state_attr("light.led_mansarda", "color_temp") <= 315 %}
                {{ state_attr('light.led_mansarda','color_temp')|int + 5 }}
              {% else %}
                {{ 320 }}
              {% endif %}
            {% else %}
                {{ 320 }}
            {% endif %}
        target:
          entity_id: light.led_mansarda
    button_right_short:
      - service: light.turn_on
        data:
          transition: 0.1
          color_temp: |-
            {% if state_attr("light.led_mansarda", "color_temp") != None %}
              {% if state_attr("light.led_mansarda", "color_temp") >= 165 %}
                {{ state_attr('light.led_mansarda','color_temp')|int - 5}}
              {% else %}
                {{ 160 }}
              {% endif %}
            {% else %}
                {{ 160 }}
            {% endif %}
        target:
          entity_id: light.led_mansarda