Template light services turn_on and turn_off behaving same as toggle

Hello all,

I have the following entity defined light.hull which works just fine.
Recently I created an automation to turn off all lights when I leave home using the turn_off service.
To my surprise when I tested the automation, every time I run the actions all the actual lights would turn off as expected.

All the template lights I have including the one below toggled state. So turn ON if was already OFF.

To clarify, the respective entity does change state and icon
Off image
On image
so I am not sure if there is something I am doing wrong or if this is some sort of bug.

In case this makes any difference I am using a Sonoff Dual R3 to control a two way light.
The binary_sensor.hall_state is on when there is power consumed in any of the 2 relays. So I am using this to then show a light entity reflecting if the light is On or Off.

- platform: template
  lights:
    hall:
      friendly_name: "Hall"
      value_template: >-
        {% if is_state('binary_sensor.hall_state', 'on') %}
          true
        {% else %}
          false
        {% endif %}
      turn_on:
        service: button.press
        target:
          entity_id: button.hall
      turn_off:
        service: button.press
        target:
          entity_id: button.hall

I’m not following, are you looking for a solution?
It seems that the automation is working like you want?

I edited the post to make it clearer.

Looking for a solution.

When I am calling a turn_off service I am getting a toggle service behaviour.

I want the template light to turn OFF only. Never turn ON when calling the turn_off service.

How do you expect it to behave differently from the turn_on service when it calls the same service call (button.press) for the same button (button.hall)?

If the state is OFF the turn_off service should not be executed.
So the expected behaviour would be no action at all.
Else how to distinguish which service to execute if not take into account the state.

You can use conditions in your light config:

- platform: template
  lights:
    hall:
      friendly_name: "Hall"
      value_template: "{{ is_state('binary_sensor.hall_state', 'on') }}"
      turn_on:
        - condition: state
           entity_id: light.hall
           state: 'off'
        - service: button.press
          target:
            entity_id: button.hall
      turn_off:
        - condition: state
           entity_id: light.hall
           state: 'on'
        - service: button.press
          target:
            entity_id: button.hall

I also simplified your value template.

2 Likes

By checking the state of binary_sensor.hall_state in both the turn_on and turn_off. Effectively it’s what tom_l suggested (but using a different entity).

1 Like

Brilliant. That worked!
Thanks a lot for the help guys!

That makes me wonder, where do you get this format from? I just checked the “template light” documentation and I don’t see any reference to “condition”

- condition: state
    entity_id: light.hall
    state: 'off'

Although this says “an action”

Screenshot 2022-04-02 at 19-01-53 Template Light

It can be a list of actions.

Anything you can put in an automation’s actions (or script sequence) can be put there.

In this case it was: https://www.home-assistant.io/docs/scripts#test-a-condition

I see. That helps :smiley: for future references.
Thanks again!

The behaviour I noticed in my original post, though, is wrong behaviour. No?
I mean I did follow the template guide and the behaviour was not exactly the expected one. So wondering should I raise a bug ticket somewhere. If so where?

No don’t do that.

It worked as expected.

The problem arose because you are using the same turn on and turn off action without checking the light state. Look at the example, it uses separate actions:

        turn_on:
          service: script.theater_lights_on
        turn_off:
          service: script.theater_lights_off

So it does not need to check for the state. Of if it does, then it does it in the scripts.

Ok, I understand now I think. It sounds unintuitive to me which is why I thought there must be a bug.
I was under the impression that when you call e.g. the turn_off service the state is automatically checked and only if is, in this example, ON then it is executed.
It is clear to me that that is not the case.

Again, thanks for educating me :sweat_smile:

Nope. That logic is up to you to implement.