Automation help with long press

Hi.

I’m a newbie to Home Assistant and automations, so apologies if this is a simple question.

I’m trying to set up an automation that switches off all lights on a long press of a switch. The switch device is a sonoff mini. When I do the long press on the switch, the MQTT message sent, which I can see in the console in Tasmota is:

stat/tasmota_EA268D/RESULT = {"Switch1":{"Action":"HOLD"}}

My automation code looks like this:

- id: '1624024642008'
  alias: Turn off all lights
  description: ''
  trigger:
  - platform: mqtt
    topic: "stat/tasmota_EA268D/RESULT"
  condition:
    condition: template
    value_template: "{{ trigger.payload_json['Switch1']['ACTION'] == HOLD }}"
  action:
  - service: light.turn_off
    target:
      entity_id: light.little_house_lights
  mode: single

However, the automation is running even on the short presses of the switch as well as the long, so clearly my condition is not being evaluated correctly.

Please can anyone see what’s wrong with it?

Thanks.

‘HOLD’ in quotes, although I’d have thought that it should never work rather than always work…

Thanks for your reply, but that seems to break it.

I replaced HOLD with ‘HOLD’ and then the lights do not get turned off with a single press or a long press. The automation is not triggered at all.

I went back to HOLD, with no quotes and behavior was it was before.

Ah. ['Action'] not ['ACTION'], and you will need the quotes on 'HOLD'.

value_template: "{{ trigger.payload_json['Switch1']['Action'] == 'HOLD' }}"

Now I understand the prior behaviour — both sides of the equality test were invalid and thus equal, so the condition always passed :joy: .

Excellent, that works :slight_smile:

Many thanks!

1 Like