What's wrong in this template?

I need to turn on and off a light, based on a sensor state. This code works well:

binary_sensor:
  - platform: gpio
    filters:
      invert
    id: input_sensor
    on_state: 
      then:
        - homeassistant.service: 
            service: light.toggle
            data:
              entity_id: "light.ikea_lucecameretta_light"

but I dont know the initial state of the sensor, then I cant use toggle. This code compiles but nothing happen to the light:

binary_sensor:
  - platform: gpio
    filters:
      invert
    id: input_sensor
    on_state: 
      then:
        - homeassistant.service: 
            service: >
              {% if id(input_sensor).state %}
                light.turn_on
              {% else %}
                light.turn_off
              {% endif %}
            data:
              entity_id: "light.ikea_lucecameretta_light"

I found the same case in the official docs: Service Calls - Home Assistant

I think the issue is the Jinja in the service call, not sure that gets passed to HA correctly. Why not use on_press: and on_release:?

binary_sensor:
  - platform: gpio
    filters:
      invert
    id: input_sensor
    on_press:
      then:
        - homeassistant.service: 
            service: light.turn_on
            data:
              entity_id: "light.ikea_lucecameretta_light"
    on_release:
      then:
        - homeassistant.service: 
            service: light.turn_off
            data:
              entity_id: "light.ikea_lucecameretta_light"

Hi Daryl,
thanks for your reply.
I canno t use on_press/on_release just because I have switches and not buttons. And I cannot use buttons because I have deviators (multiples switches controlling the same light)

The binary sensor code doesn’t care if they are switches or buttons, the events fire just the same. I can see you may have some logic hurdles with multiple switches though.

Try this:

    on_state:
      then:
        if:
          condition:
            binary_sensor.is_on: input_sensor
          then:
            - homeassistant.service: 
                service: light.turn_on
                data:
                  entity_id: "light.ikea_lucecameretta_light"
          else:
            - homeassistant.service: 
                service: light.turn_off
                data:
                  entity_id: "light.ikea_lucecameretta_light"

Sorry for asking, but is there any specific reason, why you want that automation on the ESP device and not in HA? I can’t see any advantage to outsource this to the ESP device in your case.

If you want the automation to run independent from a connection to HA, you can’t use a HA sensor. If you have the connection, why outsource it? Not to mention you’re spreading your automations throughout at least two places (=> maintenance).

1 Like

Hi paddy,
my reason is that I want my switches working if HA is not running (wifi problems, hw problems etc…). If I have a connection to HA, then I use it to command the zigbee bulb and the internal relay is always on, otherwise I just use the relay to cut or connect he power.

If my minir4 fails, I’ll lose just one 1 bulb, in your way, if HA fails, I’ll lose all the lights in my home, and this is not acceptable.

But that’s what I’m saying, the way you want it is different to what you’re trying to do here. :slight_smile:

If you want it to run without a HA connection present, you can’t use variables (entities) from HA… :wink:

If you want it depending on that variables (entities), you always need the connection… :wink:

See where I’m going here? I understand what you mean, I do it that way myself with some Shellys, as I want all my switches to run without HA. Btw you should take a look at the Shelly1, if you’re interested in something like that. :wink:

Yes, I got your point. The reson of my answer is … well in the meanwhile I learned a little more on HA and I figured out how to perform the goal:

    on_state: 
      if:
        condition:
          api.connected:
        then:
          - homeassistant.service:
              service: light.toggle
              data:
                entity_id: "light.lucisala_group"
        else:
          - switch.toggle: relay  
    on_double_click:
      then:
        - switch.toggle: relay

@zoogara The on_press and on_release events bind the action to the actual state of input sensor (1 or 0), but I can control the lights by Google Home, HA app etc… without operate the switch. Using those events result in a more complex automation, because I should sync the input witch the actual state of the bulb when I ask to Google Home to switch the lights on. For instance: the light is on, I switch it off by a voice comand. Now the binary_sensor has a “true” logic level but the bulb is off. If I use now the the switch, I want to turn on the bulb, but the binary sensor will go to false and nothing happen.

The on state event is fired when the state change, no matter what state, then I use the toggle action…

Now I finally get, what you meant with only relay… :wink: Thanks, looks inspiring! :slight_smile: And good you got it working! :+1: