Template Light in Siemens S7 1200

To control the lights, I use the Siemens S7 1200 automation, which I connected to Home-assistant via Nodred. I use a button to turn the lights on and off. The output relay on the Siemens Controller is responsible for the state of the lights. I created a light template that shows me the state of the light (on and off). I would also like to include in the template that with the command turn on the light, the light turns on only if it is turned off.

Now when I turn on the light, it switches (on if it’s off and off if it’s on)

Has anyone had such a case?

Here is my template example:

- platform: template
  lights:
    ##### Luč kabinet ###########################
    luc_kabinet:
      friendly_name: "Kabinet"
      unique_id: 7b1c9ca3-036a-492d-82b0-ed0265e300e3
      value_template: "{{ is_state('binary_sensor.siemens_q9_4', 'on') }}"
      turn_on:
        service: input_button.press
        data: {}
        target:
          entity_id: input_button.gumb_1
      turn_off:
        service: input_button.press
        data: {}
        target:
          entity_id: input_button.gumb_1

In the automation, I check the status of binary_sensor.siemens_q9_4 and the automation works. I would like to be able to check this status in the template as well.

Here is example of my automation.

alias: Sončni vzhod - Luč kabinet - izklop
description: xxxxxx
trigger:
  - platform: sun
    event: sunrise
    offset: 0
condition:
  - condition: state
    entity_id: binary_sensor.siemens_q9_4
    state: "on"
action:
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.luc_kabinet
mode: single

Im asking for help

I tried to add a condition to the code, but when checking, I get that the settings are not valid:
Have anyone any ideas?


Invalid config for [light.template]: value should be a string for dictionary value @ data[‘lights’][‘luc_kabinet’][‘turn_off’][1][‘service’]. Got {‘service’: ‘input_button.press’, ‘data’: {}, ‘target’: {‘entity_id’: ‘input_button.gumb_1’}}
value should be a string for dictionary value @ data[‘lights’][‘luc_kabinet’][‘turn_on’][1][‘service’]. Got {‘service’: ‘input_button.press’, ‘data’: {}, ‘target’: {‘entity_id’: ‘input_button.button_1’}}. (See ?, line ?).

- platform: template
  lights:
    luc_kabinet:
      friendly_name: "Kabinet"
      unique_id: 7b1c9ca3-036a-492d-82b0-ed0265e300e3
      value_template: "{{ is_state('binary_sensor.siemens_q9_4', 'on') }}"
      turn_on:
        - condition:
            condition: state
            entity_id: binary_sensor.siemens_q9_1
            state: "off"
        - service:
            service: input_button.press
            data: {}
            target:
              entity_id: input_button.gumb_1
      turn_off:
        - condition:
            condition: state
            entity_id: binary_sensor.siemens_q9_1
            state: "on"
        - service:
            service: input_button.press
            data: {}
            target:
              entity_id: input_button.gumb_1