Custom Toggle Automation

Hello everyone. I like to create a custom toggle automation, because I may not want to go back to the previous state - instead I like to switch on/off light with a certain brightness. I use a single switch, hitting it once shall turn on the light if it was in the ‘off’ state previously and vice versa.

I tried to use a service_template, but I have to deliver a “brightness” if the light.turn_on command was used. But! I’m not allowed to deliver the “brightness” if the light.turn_off command was used. Thus, I tried to use an additional data_template but I don’t have any succes with this. I’m getting all sorts of errors like “while scanning for the next token found character ‘%’ that cannot start any token”. This is surely just a syntax-error… Here is what I tried so far:

  • alias: SwitchingBedroomLight
    trigger:
    platform: event
    event_type: click
    event_data:
    entity_id: binary_sensor.switch_158xxxxxxxxxxx
    click_type: single
    action:
    • service_template: >
      {% if is_state(‘light.hue_color_lamp_2’,‘off’) %}
      light.turn_on
      {% else %}
      light.turn_off
      {% endif %}
      data_template: >
      {% if is_state(‘light.hue_color_lamp_2’,‘off’) %}
      brightness: 255
      {% endif %}
      entity_id: light.hue_color_lamp_2

I hope someone is willing to help me, thanks in advance.

Try this and mention the brightness: > instead of data_template: >

  - alias: SwitchingBedroomLight
      trigger:
        platform: event
        event_type: click
        event_data:
        entity_id: binary_sensor.switch_158xxxxxxxxxxx
        click_type: single
      action:
        service_template: >
          {% if is_state(‘light.hue_color_lamp_2’,‘off’) %}
            light.turn_on
          {% else %}
            light.turn_off
          {% endif %}
      data_template:
        entity_id: light.hue_color_lamp_2
        brightness: >
          {% if is_state(‘light.hue_color_lamp_2’,‘off’) %}
            255
          {% endif %}

Thank you for looking at this. However, it’s still not working:

Starting with the light switched off, pushing the button lets the light turn on. Pushing the button a second time give the following error message:

[homeassistant.core] Invalid service data for light.turn_off: extra keys not allowed @ data[‘brightness’]. Got ‘’

I guess, that the keyword “brightness” is still present, but just without a value assigned… right? So in this case I have to hide the whole keyword “brightness” instead of its value… but how to do so?

Ah, you’re right. Brightness is not supported for light.turn_off, struggled with this also a while ago. It’s possible to ‘work around’ this with scenes, or make two automations…

Sorry :frowning:

would something like this work?
I don’t know how HA reacts if the service is blank in a service_template.

  - alias: SwitchingBedroomLight
      trigger:
        platform: event
        event_type: click
        event_data:
        entity_id: binary_sensor.switch_158xxxxxxxxxxx
        click_type: single
      action:
        service_template: >
          {% if is_state(‘light.hue_color_lamp_2’,‘off’) %}
            light.turn_on
          {% endif %}
      data_template:
        entity_id: light.hue_color_lamp_2
        brightness: >
          {% if is_state(‘light.hue_color_lamp_2’,‘off’) %}
            255
          {% endif %}
      action:
        service_template: >
          {% if is_state(‘light.hue_color_lamp_2’,‘on’) %}
            light.turn_off
          {% endif %}
      data:
        entity_id: light.hue_color_lamp_2