Shelly automation - how to trigger?

Hi all,
I’m very new to HA, riding on the learning curve… It may well be that the following is HA trivia, but I haven’t figured it out so far so here goes.
HA 2021.7.2, bundled Shelly integration.
I have a few Shelly devices in a lab, with switches / loads / whatnot and am trying to create a trivial automation on a Shelly 2.5 switch: If channel 1 on the Shelly is on for more than 5 seconds, turn it off. The thing is, I’d like for this to work even if HA (re)starts and finds the channel already “on”.

I made the simple automation pasted below. So as not to miss events, it is based on looking at the power going through the switch to the load. It mostly works: When I switch the channel to on, it shuts it off after 5 seconds. However when I (a) disable the automation (b) switch the channel to “on” (c) enable the automation, it doesn’t kick in.

I’m sure I’m missing something quite basic re how triggers and events are processed. It appears as if the automation is triggered when the Shelly sends an event (switch turned on), but not via ongoing polling.
Is the Shelly integration even supposed to poll the device, or am I being stupid? Is there a proper way to achieve this?

Thanks in advance for any pointer or insight.

id: '1626264395292'
alias: Turn it off
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.the_light_channel_1_power
    above: '0.3'
    for: '00:00:05'
condition: []
action:
  - type: turn_off
    device_id: d<redacted>d
    entity_id: switch.the_light_channel_1
    domain: switch
mode: single

Because at that point, the physical device is already on and consuming more power than 0.3.

A Numeric State Trigger will trigger the moment when the value (power) increases and crosses the threshold value of 0.3. It doesn’t trigger if the value is already above the threshold.

2 Likes

Hello.

For the numeric_state trigger, the value needs to be under 0.3 and then go passing that point for the automation to be triggerred. It won’t trigger it if the value is already above 0.3.

You can add this trigger-

trigger:
  - platform: homeassistant
    event: start

And for the action you can add-

action:
  - service_template: >
      {% states('sensor.the_light_channel_1_power') > '0.3' %}
        switch.turn_off
      {% endif %}
    entity_id:
      - switch.the_light_channel_1

Let me know how it works out.

Your template doesn’t consider the possibility when, on startup, the power value is not greater than 0.3. If that happens the template returns nothing which represents an invalid service call.

1 Like

Ah yes that is true, any suggestion? Maybe by adding this?

condition:
  - condition: template
    value_template: '{{ states('sensor.the_light_channel_1_power') > '0.3' }}'

Therefore it won’t run unless the value is greater than 0.3?

1 Like

It all depends on doron’s requirements.

For example, the suggested correction is based on detecting the restart of Home Assistant and not for what doron tried (enabling/disabling the automation). Do we know if that was only for testing purposes or a requirement? Doron needs to clarify the requirements.

In addition, on startup should the device be turned off after 5 seconds if power is greater than 0.3, or immediately if the device was already on for more than 5 seconds while Home Assistant was offline?

Thanks! This is really helpful, I missed that.
Is there a different way to say “if this switch is seen to be on, FOR more than time X, then do Y”?
(I resorted to looking at power consumption only due to this consideration, i.e. missing the move from off to on due to any reason).

Thank you! Great stuff.
So, this will run once upon HA startup, correct?
What about when the automation is disabled and then enabled? Or, in general, when HA just missed the event of “move from off to on” for any reason?
What I’m trying to get at is an automation that will work reliably, regardless of whether it witnessed the powering on. I’m thinking robustness. I hope I’m being reasonable - as I said I’m learning.

It won’t detect enabling/disabling the automation (is that a requirement?) and it will generate an error message under certain conditions (described in my previous post).

trigger:
  - platform: homeassistant
    event: start
  - platform: numeric_state
    entity_id: sensor.the_light_channel_1_power
    above: '0.3'
    for: '00:00:05'

condition:
  - condition: template
    value_template: '{{ states('sensor.the_light_channel_1_power') > '0.3' }}'

action:
  - service_template: >
      {% states('sensor.the_light_channel_1_power') > '0.3' %}
        switch.turn_off
      {% endif %}
    entity_id:
      - switch.the_light_channel_1

By using this, it will trigger on HA restart. However, on restart, it will not count until 5 seconds for the switch to be turned off - just switched off directly after HA restart.

It still contains the bug I mentioned earlier.

Replace the use of a service_template with a choose that executes the service call only if power exceeds 0.3.

You should also convert the sensor’s value to float and compare it to a floating-point number, not a string. String comparisons of floating-point numbers can produce erroneous results.


BTW, service_template was deprecated in favor of simply service (many versions ago).

Would it be simpler if I’d not be looking at power, but at the switch state being “on”? As I said the reason I was looking at power to begin with was my thinking about missing the flip event. But that happened anyway :slight_smile: So maybe I don’t need it? (the 0.3w is just made up, as in > 0 ).

Thanks! Didn’t know if service_template is already deprecated as I was always referencing from automation template from the forum (maybe an old one).

A bit simpler.

What complicates it is the need to turn it off after 5 seconds of operation even at startup and enabling/disabling.

I can’t recall if there is an event produced when an automation is enabled/disabled. If there isn’t then nothing is available to trigger the automation when you re-enable the automation. I just realized it wouldn’t matter. An automation that has just been enabled cannot detect it’s own enabling.

You still need to explain how you want to handle the switch on startup. Should it be turned off immediately if it’s already on or checked if it was already on for at least 5 seconds, or simply allowed to stay on for another 5 seconds?

alias: New Automation
description: ''
mode: single
trigger:
  - platform: homeassistant
    event: start
    id: HA Start
  - platform: state
    entity_id: switch.the_light_channel_1
    id: Other
    to: 'on'
    for: '00:00:05'
condition:
  - condition: state
    entity_id: switch.the_light_channel_1
    state: 'on'
action:
  - choose:
      - conditions:
          - condition: trigger
            id: HA Start
        sequence:
          - delay:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
          - service: switch.turn_off
            target:
              entity_id: switch.the_light_channel_1
      - conditions:
          - condition: trigger
            id: Other
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.the_light_channel_1
    default: []

Maybe using choose action and setting delay can be a workaround for counting 5 seconds after startup.

1 Like

Understood. So perhaps we can drop that.

Ideally - the latter; but if it’s far fetched, the former would be fine.

I believe ardysusilo has combined all of the suggestions that have been made into an automation (see above) that meets your stated requirements.

Thank you VERY much @ardysusilo and @123 - this is extremely helpful, and the very discussion helped me tremendously.
What a great community!