Automation switch condition based on user action or Logbook

Hy there!

I have an automation in HA to power ON and OFF a water heater using a sonoff s26r2 wifi switch trigger by a state and attribute condition value provided by a solar inverter (huawei sun2000l) integration.
All good, but for the power OFF automation i want to include a new condition like a “manual” scenario when a user of ha manual turn ON the switch, so automation should return false and not power off the s26r2.

In the logbook of s26r2 i see the message “Turned on by Valentin” if i manual power on the switch but how can i put this in the automation.

The actual yaml code is this. How can i integrate the user condition?

alias: PowerOff_Sun2000_WaterHeater_2kw
description: PowerOff
trigger:
  - platform: state
    entity_id: sensor.sun2000l_3ktl_2101073801hvk7000432
condition:
  - condition: device
    type: is_on
    device_id: 4341c747aa1369bd67c938050b434495
    entity_id: switch.sonoff_100144c2ff
    domain: switch
  - condition: or
    conditions:
      - condition: numeric_state
        entity_id: sensor.sun2000l_3ktl_2101073801hvk7000432
        attribute: power
        below: '-100'
      - condition: numeric_state
        entity_id: sensor.sun2000l_3ktl_2101073801hvk7000432
        attribute: power_meter_active_power
        above: '1500'
action:
  - type: turn_off
    device_id: 4341c747aa1369bd67c938050b434495
    entity_id: switch.sonoff_100144c2ff
    domain: switch
mode: single

Hello,

Maybe you could use the event trigger, with the event LOGBOOK_ENTRY ? I’ve never done it but worth a try :slight_smile:
Or easier, trigger the automation with S26R2 switch state and not the logbook entry.

1 Like

@duvalale thx for your time and help, i will try asap your suggestion. After i will post an update.

  1. The switch state option not work because there are only 2 options on/off.
  2. I tried with event trigger but not working, i don’t get how to write the code.
    So in the logbook i have the message when i manual switch in the app “S26R2 turned on by Valentin” valentin is the user. The idea is when only the user switch the automation should not turn off the s26r2.

Also the entity id of s26r2 is switch.sonoff_100144c2ff.

how should i integrate the logbook events. I tried with the code below but doesn’t work :frowning:

alias: PowerOff_Sun2000_WaterHeater_2kw
description: PowerOff
trigger:
  - platform: state
    entity_id: sensor.sun2000l_3ktl_2101073801hvk7000432
  - platform: event
    event_type: LOGBOOK_ENTRY
    event_data:
      message: S26R2 turned on by Valentin
    id: LogBook
condition:
  - condition: device
    type: is_on
    device_id: 4341c747aa1369bd67c938050b434495
    entity_id: switch.sonoff_100144c2ff
    domain: switch
  - condition: or
    conditions:
      - condition: numeric_state
        entity_id: sensor.sun2000l_3ktl_2101073801hvk7000432
        attribute: power_meter_active_power
        below: '-100'
  - condition: not
    conditions:
      - condition: trigger
        id: LogBook
action:
  - type: turn_off
    device_id: 4341c747aa1369bd67c938050b434495
    entity_id: switch.sonoff_100144c2ff
    domain: switch
mode: single

If you want to avoid a manual override, you could create a second automation that turns off the switch as long as the sensor conditions for the offstate are met. Or you could use the Restriction Card.

1 Like

Thanx for your time, but i don’t want to avoid, i want that my automation based on my code above to see as a condition that a user of HA switch the state to on and not do the automation.

Ah, ok. I’m not 100% sure if this will work the way you have in mind, but you could try to add the following parts:


trigger:
  - platform: state
    entity_id: switch.sonoff_100144c2ff
    to:


condition:
  - condition: template
    value_template: |
        {{ trigger.to_state.context.user_id == 'YOUR_USER_ID' }}