Light template not taking new values

Hello,
I am using HA to control my home automation which is located on a second “machine”.
I am currently using shell_command to control entities on that machine.

I am on 0.97.2.

I have a problem with the light_template.
The code is below.

When I change the state of the light in HA, it sends always the status_quo of the sensor reading, but not the new values (ON, OFF, or brightness) set by the HA GUI.
I remember it worked before…I am not sure if the update to 0.97.2 (from 0.96.x) might be a reason. Could not find any reports so far.
I hope someone in the forum got an idea what I am doing wrong here.

Thanx in advance.
(I hope the quotes are working as I did it, as there is no preview…)

configuration.yaml

sensor:
  - platform: command_line
    name: Dimmer Wohnen Wand
    command: /usr/bin/curl -X GET 'http://192.168.1.111:80/cgi-bin/hc.php?HC_DIM;LIGHT;RD;Wohnen_Wand'
    value_template: '{{ value[94:-10] }}'

# SHELL COMMANDS

shell_command:
  licht_wohnen_wand: /usr/bin/curl -X GET 'http://192.168.1.111:80/cgi-bin/hc.php?HC_DIM;LIGHT;WR;Wohnen_Wand;{{((state_attr("light.wohnen_wand_licht", "brightness")|int)/2.55)|round(0)}};3'

lights.yaml

- platform: template
  lights:
    wohnen_wand_licht:
      friendly_name: "Wohnen Wand Licht"
      #level_template: "{{(state_attr('light.wohnen_wand_licht', 'brightness')|int)}}"
      #value_template: "{{ states('light.wohnen_wand_licht') }}"
      level_template: "{{ '%2d' | format(states('sensor.dimmer_wohnen_wand') | int*2.55) }}"
      # wandelt 0-100 in 0-255 um was von HA fuer korrekte Anzeige Lichtwerte benoetigt wird
      value_template: >-
        {% if states('sensor.dimmer_wohnen_wand') | int > 0 %}
          on
        {% else %}
          off
        {% endif %}            
      turn_on:
        service: shell_command.licht_wohnen_wand
        data:
          entity_id: light.wohnen_wand_licht
          brightness: "100"
        #- service: shell_command.licht_wohnen_wand
      turn_off:
        service: shell_command.licht_wohnen_wand
        data:
          entity_id: light.wohnen_wand_licht
          brightness: "0"
        #- service: shell_command.licht_wohnen_wand
      set_level:
        service: shell_command.licht_wohnen_wand
        data_template:
          entity_id: light.wohnen_wand_licht
          brightness: "{{ brightness }}"

The value_template should evaluate to true if the light is on, not on.

value_template: "{{ states('sensor.dimmer_wohnen_wand') | int > 0 }}"

Hello Phil, thank you for the hint. Changed it. No difference in behavior though.
The GUI did display correctly the state of the light before and does now as well.

If I switch the light or use the brightness control, send_command sends the current value of the light not the new one. Also the template editor in HA which use to check what happens to the entities does not reflect the new brightness value.
it is even like that, that the brightness slider is at a new value (e.g.50%) bit the template editor still shows the actual brightness of the entity.
This looks very strange to me.
I must do something very basic wrongly.

I think your shell_command and the way you’re calling it are the problem. You’re passing two parameters to the shell_command: entity_id and brightness. However, in the shell_command itself you’re not using either of those values. Rather you’re using the current brightness.

Try this:

shell_command:
  licht_wohnen_wand: /usr/bin/curl -X GET 'http://192.168.1.111:80/cgi-bin/hc.php?HC_DIM;LIGHT;WR;Wohnen_Wand;{{ (brightness/2.55)|round(0) }};3'

And in the light template, in the turn_on, turn_off & set_level parameters, you can remove the entity_id lines, since they don’t do anything.

Hello Phil,
I deleted the entity_id stuff from the light_template and changed the shell_command part.

However I didn’t understand the shell_command hint. It is only brightness that I am passing to the shell command…

It did not change anything.

In the meantime I tested something more basic, as I think something is really not working as I think it should.
For this is created dummy light and only want notifications to see what changes when turning on, turning off and setting a level.

- platform: template
  lights:
    dummylight:
      friendly_name: "dummylight"
      #level_template: '{{ state_attr("light.dummylight", "brightness") }}'
      #value_template: '{{is_state("light.dummylight", "on")}}'
      
      turn_on: 
        - service: persistent_notification.create
          data_template:
            message: "{{ brightness }}"
       
      turn_off:
        - service: persistent_notification.create
          data_template:
            message: "{{ brightness }}"
       
      set_level:
        service: persistent_notification.create
        data_template:
          message: "{{ brightness }}"

This is in my lights.yaml
What happens in the GUI is:

turning light on: icon of light is yellow, brightness level stays off, notification: a blank. state_attr for brightness says: none

turning light off: icon of light is grey, rest like love for turning on.

setting brightness level to 51%: icon of light is dark yellow, brightness level says 130, notification: 130. state_attr for brightness says: 130 -> works as expected

turning off from here leads to the state described for “turning light off”
Turning on again from here setts the light to the 51% etc (so turning on once you set a brightness level returns to previous state)

If I try now to set brightness levels for "light.dummylight “turn_on” and “turn_off” like 255 and 0 on my config file leads to:
turn_on: notification shows two times with 255 (but why?), bulb is on
turn_off: infinite loop of executing step call service…I have to stop HA completely as otherwise it goes on forever. Changing the value to “1” has the same effect.

Anyway, this is really starting to frustrate me. It looks like it must be something basic…possibly I have to use the state objects stuff to identify previous state and updated state.

:upside_down_face:

I found the solution…below.
The only issue that remains is, that after I turned on or off the light due tot that it is a dimmer the update of the light level is earlier than the time for 100% or 0% light level. Therefore it shows at first the wrong light level after I have switched the light.
I have to check, if there is some solution for this (delaying the sensor polling or similar)

- platform: template
  lights:
    wohnen_wand_licht:
      friendly_name: "Wohnen Wand Licht"
      #level_template: '{{(state_attr("light.wohnen_wand_licht", "brightness")|int)}}'
      #value_template: >-
        #{% if (state_attr("light.wohnen_wand_licht", "brightness"))|int>0 %}
          #on
        #{% else %}
          #off
        #{% endif %}
          
      level_template: '{{ "%2d" | format(states("sensor.dimmer_wohnen_wand") | int*2.55) }}'
      # wandelt 0-100 in 0-255 um was von HA fuer korrekte Anzeige Lichtwerte benoetigt wird
      value_template: '{{ states("sensor.dimmer_wohnen_wand")|int >0 }}'
      #level_template: '{{state_attr("light.wohnen_wand_licht", "brightness")}}'
      #value_template: '{{is_state("light.wohnen_wand_licht", "on")}}'
      
      turn_on:
        - service: shell_command.licht_wohnen_wand
          data_template:
            value: 255
        - service: homeassistant.update_entity
          entity_id: sensor.dimmer_wohnen_wand
        
      turn_off:
        - service: shell_command.licht_wohnen_wand
          data_template:
            value: 0
        - service: homeassistant.update_entity
          entity_id: sensor.dimmer_wohnen_wand
            
      set_level:
        service: shell_command.licht_wohnen_wand
        data_template:
          value: "{{ brightness }}"