Service based on light state

Hello community, I’m trying to create an automation that should work as following:

If nightlight (it’s yeelight state) is on, use light.turn_off as service, otherwise use light.toggle.

I’m using the following action in automation, but it doesn’t work:

  action:
    - service_template: >
        {% if is_states(light.bagno_nightlight == 'on') %}
          light.turn_off
        {% else %} 
          light.toggle
        {% endif %}
      data_template:
        entity_id: light.bagno

Any advice please?
Thank you,
Lucas

Do you realise that is the is the same as always using the toggle service?

If on, light.turn_off (== toggle the light).
Else if off, toggle.

@tom_l - it’s a different light that’s opposite-mirroring

@Lucas_Rey

action:
  service : "light.turn_{{ 'off' if is_state('light.bagno_nightlight', 'on') else 'on' }}"
  entity_id: light.bagno
1 Like

Surely

action:
  service: "light.{{ 'turn_off' if is_state('light.bagno_nightlight', 'on') else 'toggle' }}"
  entity_id: light.bagno

(updated services, removed space after service)

@Lucas_Rey your original was fine except for this line:

{% if is_states(light.bagno_nightlight == 'on') %}

which should have been:

{% if states('light.bagno_nightlight') == 'on' %}
2 Likes

@anon43302295, thank you for the input, but still doesn’t work, when the light is totally off it doesn’t switch on, and when the nightlight is on, it switch on the normal light.

Basically I need :
light.toggle when the nightlight is off
light_turn_off when the nightlight is on

OK, have you tried @Troon’s version that uses toggle instead of off?

I think your issue is, that nightlight is an attribute of the yeelight bulb, not the state of the bulb itself, isn’t it?

@anon43302295: Yes, it’s seem working now, I’m testing the combinations. The fact is that I have other authomation who switch on the bathroom nightlight during the night basing on binary_sensor.presence switch, and normal light in the rest of the day. I have also a xiaomi button to toggle the light, and with switch toggle, when the nightlight is on, and I use toggle with xiaomi button, it just switch on the normal light, then I have to press again to switch off.
In this way, if the nightlight will be switched off immediately. I hope I was clear :slight_smile:

@Burningstone: I don’t think so, nightlight acts as a “pure” light, normally I can use toggle/on/off without issue.

For now, thank you all! :slight_smile:

1 Like

@Troon:

your original was fine except for this line:

{% if is_states(light.bagno_nightlight == 'on') %}

which should have been:

{% if states('light.bagno_nightlight') == 'on' %}

I also tired the states instead is_states, but it doesn’t work, I don’t know why. However, your code seems perfect, so thank you.

1 Like

You could also have used:

{% if is_state('light.bagno_nightlight', 'on') %}

Note is_state not is_states, and you provide the test value as the second argument, not with a == test.

I am always amazed how far people get with this system, when English is not their first language.

1 Like