Automation on device triggered by Home Assistant entity

Hi everybody,

can you please help me create an automation that will run on my ESPHome device triggered by an input_boolean from Home Assistant?

Home Assistant → test_powerbank.yaml

input_boolean:
  test_powerbank:
    name: "Powerbank LED"
    icon: "mdi:led-outline"

ESPHome (nodeMCU v2) relevant code

text_sensor:
  - platform: homeassistant
    id: power_led
    entity_id: input_boolean.test_powerbank

# (...)

deep_sleep:
  run_duration: 30sec # (currently 10min for testing purposes)
  sleep_duration: 15min

light:
  - platform: binary
    name: Powerlight
    output: light_output

output:
  - id: light_output
    platform: gpio
    pin:
      number: D5

The idea is that this device will be powered off most of the time; when it wakes up, it should check whether input_boolean.test_powerbank is on or off. Depending on this, it should switch the id: light_output light (or, for the actual project I am planning, a switch) to the same state. So if input_boolean.test_powerbank == on, the light (later switch) should set itself to the same state - and vice versa for off.

I tried adding this underneath entity_id: input_boolean.test_powerbank, but that did not work.

    on_turn_on:
      light_turn_on:
        - id: light_output
    on_turn_off:
      light_turn_off:
        - id: light_output

I assume it could be solved by using templates-lambdas, but I don’t seem to understand the syntax and/or just cannot make this work.

Thank you in advance for your ideas :slight_smile:

(btw., is this the right approach? The idea is to connect this device to both a powerbank and to a 3V3 fairy lights strip (powered by 2 or 3 AA batteries); if one wants them to be on, they change the switch on Home Assistant - then wait for the ESPHome device to wake up, check the status, and act accordingly. Switching is supposed to be done via gpio switch and an 2N2222 transistor that will be connected to one pin on the nodemcu and replace the physical on/off switch on that fairy lights power supply)

EDIT: bonus question: IF we were to use templates-lambdas, anyway, perhaps there could be an addition: if Home Assistant cannot be reached (no wifi, Home Assistant down, or any other reason), also turn the fairy lights to off. So if they are on and something goes wrong, they will still be switched off upon the next wake up.

I decided to enable or disable automation right in esphome … created a script

script:                          
  - id: rfid_open
    mode: restart
    then:
      - if:
          condition:
            - switch.is_on: card_am
          then:    
            - switch.turn_on: relay
            - rtttl.play: '1:d=16,o=6,b=95:32d,32d#,32d,32d'
            - delay: 3s
            - switch.turn_off: relay
          else:
            - rtttl.play: 'error:d=16,o=6,b=95:45d,45d,45d,45d,45d,45d,45d,45d,45d,45d,45d,45d,45d,45d'
            #- rtttl.play: 'error:d=16,o=5,b=95:45d,45d,45d,45d,45d,45d,45d,45d,45d,45d,45d,45d,45d,45d'


Then I created a switch

switch:
  
  - platform: template        
    name: "Card a/m"
    id: card_am
    #restore_state: true
    optimistic: true

If the switch is on, the script is executed:

binary_sensor:
  - platform: rc522                  
    uid: 17-6D-00-95
    id: test_tag
    name: "RC522 RFID Tag"
    on_press:
      - script.execute: rfid_open 

You’re on the right path, but what you want is the homeassistant binary sensor, not text sensor (since in HA you’ve a boolean input, not a text input).

binary_sensor:
  - platform: homeassistant
    id: power_led
    entity_id: input_boolean.test_powerbank
    on_press:
      light_turn_on:
        - id: light_output
    on_release:
      light_turn_off:
        - id: light_output

Now that I’m looking at the docs for this, the on_press/on_release names (and their docs) seem really obscure and unintuitive… We should change that.

Thank you. I first switched to this,

binary_sensor:
# (...)
  - platform: homeassistant
    id: power_led
    entity_id: input_boolean.test_powerbank
    on_press:
      light_turn_on:
        - id: light_output
    on_release:
      light_turn_off:
        - id: light_output

which produced this output

Failed config

binary_sensor.homeassistant: [source powerbank.yaml:42]
  platform: homeassistant
  id: power_led
  entity_id: input_boolean.test_powerbank
  on_press:  [source powerbank.yaml:46]

    Unable to find action with the name 'light_turn_on'.
    light_turn_on:  [source powerbank.yaml:47]
      - id: light_output
  on_release:  [source powerbank.yaml:49]

    Unable to find action with the name 'light_turn_off'.
    light_turn_off:  [source powerbank.yaml:50]
      - id: light_output

This variant below will produce the output below

  - platform: homeassistant
    id: power_led
    entity_id: input_boolean.test_powerbank
    on_press:
      light.turn_on:
        - id: light_output
    on_release:
      light.turn_off:
        - id: light_output

=>

Failed config

binary_sensor.homeassistant: [source powerbank.yaml:42]
  platform: homeassistant
  id: power_led
  entity_id: input_boolean.test_powerbank
  on_press:  [source powerbank.yaml:46]

    string value cannot be dictionary or list.
    light.turn_on:  [source powerbank.yaml:47]
      - id: light_output
  on_release:  [source powerbank.yaml:49]

    string value cannot be dictionary or list.
    light.turn_off:  [source powerbank.yaml:50]
      - id: light_output

I also tried as mentioned in the other reply

  - platform: homeassistant
    id: power_led
    entity_id: input_boolean.test_powerbank
    on_press:
      light.turn_on: light_output
    on_release:
      light.turn_off: light_output

=>

Failed config

binary_sensor.homeassistant: [source powerbank.yaml:42]
  platform: homeassistant
  id: power_led
  entity_id: input_boolean.test_powerbank
  on_press:  [source powerbank.yaml:45]
    - [source powerbank.yaml:45]
      then:  [source powerbank.yaml:45]
        - [source powerbank.yaml:45]
          light.turn_on:  [source powerbank.yaml:45]

            ID 'light_output' of type gpio::GPIOBinaryOutput doesn't inherit from light::LightState. Please double check your ID is pointing to the correct value.
            id: light_output [source powerbank.yaml:45]
            state: True
  on_release:  [source powerbank.yaml:47]
    - [source powerbank.yaml:47]
      then:  [source powerbank.yaml:47]
        - [source powerbank.yaml:47]
          light.turn_off:  [source powerbank.yaml:47]

            ID 'light_output' of type gpio::GPIOBinaryOutput doesn't inherit from light::LightState. Please double check your ID is pointing to the correct value.
            id: light_output [source powerbank.yaml:47]
            state: False
  disabled_by_default: False
  name: power_led
  internal: True

I am usually able to pick up on what causes the problem when working with Home Assistant, but ESPHome yaml is still a bit confusing to me.

Once again, thanks for sharing, couldn’t figure out how to disable my automation script for a certain amount of time, your solution worked out great, added a turn_on action for the switch after a delay.

sensor:
  - platform: dallas
    address: 0x4501205f9eb64628
    name: "Probe 01"
    id: probe01
    filters:
      - lambda: return x * (9.0/5.0) + 32.0;
      - offset: -0.6
    unit_of_measurement: "°F"
    on_value_range:
      - above: 82
        then:
          - script.execute: alarm_script
      - below: 78
        then:
          - script.execute: alarm_script

switch:
  - platform: template        
    name: "Script Control"
    id: script_control
    optimistic: true
    on_turn_off:
    - delay: 300s
    - switch.turn_on: script_control

script:
  - id: alarm_script
    then:
      - if:
          condition:
            - switch.is_on: script_control
          then:
            - repeat:
                count: 99
                then:
                  - switch.toggle: buzzer
                  - delay: 1s
                  - switch.toggle: buzzer
                  - delay: 1s

button:
  - platform: template
    name: "Alarm Reset"
    on_press:
      then:
      - script.stop: alarm_script
      - switch.turn_off: script_control
      - if:
          condition:
            switch.is_on: buzzer
          then:
            - switch.turn_off: buzzer