Automation settings

How would I set up an Automation that turns a Device (a smart plug) on if an Entity (a Shelly device monitoring PV production) has a value higher than 30, and turns it off below this value. I am getting in a mess with regards to whether to use Triggers or Conditions.

The values produced by the Entity range from ~ -20 to ~ +600. Ideally I would also like to insert a buffer so that when the value is hovering around 30 it doesn’t continually turn the Device On & Off.

This is what I have done so far, but it isn’t doing what I want it to do …

TIA !

- id: '16206339676767'
  alias: Battery Charger ( On)
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.barns_pv_power
    attribute: unit_of_measurement
    from: '30'
  condition: []
  action:
  - type: turn_on
    device_id: bda678a465f2c799ea389bdf3a2c64a9
    entity_id: switch.bf5e20d4e26f5fcd03ykww
    domain: switch
  mode: single

That trigger fires when the sensor’s unit_of_measurement attribute changes state from exactly 30 to anything else — not a likely scenario. Why did you include the attribute? Did you think the UI was “forcing” you to?

I’d suggest using numeric state:

trigger:
  - platform: numeric_state
    entity_id: sensor.barns_pv_power
    above: '30'

and then a second automation to switch the device off again if the power drops below a certain amount, perhaps 25?

You can also use for to say how long this trigger state must be true before firing. Full example:

- id: '1'
  alias: Battery Charger (On)
  trigger:
  - platform: numeric_state
    entity_id: sensor.barns_pv_power
    above: '30'
    for:
      minutes: 1
  action:
  - type: turn_on
    device_id: bda678a465f2c799ea389bdf3a2c64a9
    entity_id: switch.bf5e20d4e26f5fcd03ykww
    domain: switch

- id: '2'
  alias: Battery Charger (Off)
  trigger:
  - platform: numeric_state
    entity_id: sensor.barns_pv_power
    below: '25'
    for:
      minutes: 1
  action:
  - type: turn_off
    device_id: bda678a465f2c799ea389bdf3a2c64a9
    entity_id: switch.bf5e20d4e26f5fcd03ykww
    domain: switch
1 Like

Many thanks @Troon for your reply - much appreciated. I can only think you are right re: me being steered towards the exact rather than above.

But the following still doesn’t get it to do what I would like - but I am not sure why as it appears to be the same / “right”, except I did try two alternative ways to get the 1 minute delay to work. I have treble checked the device IDs etc, and have monitored the “state” within the Developers tab.

- id: '1620633961184'
  alias: Battery Charger ( On)
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.barns_pv_power
    for: 0:01:00
    above: '30'
#    for:
#      minutes: 1
  action:
  - type: turn_on
    device_id: bda678a465f2c799ea389bdf3a2c64a9
    entity_id: switch.bf5e20d4e26f5fcd03ykww
    domain: switch
  mode: single
- id: '1620639788311'
  alias: Battery Charger (Off)
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.barns_pv_power
    for: 0:01:00
    below: '25'
  condition: []
  action:
  - type: turn_off
    device_id: bda678a465f2c799ea389bdf3a2c64a9
    entity_id: switch.bf5e20d4e26f5fcd03ykww
    domain: switch
  mode: single

Suggest going to the Automations page and checking the following

  1. Do either of the automations get triggered (visible in the Last Triggered column) - If not, remove the time condition, and then re-check to see if that affects anything
  2. If they do get triggered, use the trace feature to see what happens during the automation (middle icon of the 3 on the right)

Don’t forget after changing anything to reload the automations. (I’ve made that mistake before and wondered why the code changes I’d made had no effect :slight_smile: )

1 Like

Thanks @p4mr ; removing (or not) the time delay doesn’t make any difference. Interestingly if I Run Actions on both the ON and OFF within the Automations tab, the smart plug does turn on & off, so manually firing the automations does what I want them to do, but they don’t work automatically.

I’ll be honest & admit that I didn’t know about the RELOAD Automation box, but I do always restart HASS after making any changes, and half hoped that did the same thing.

Clicking on the Trace button doesn’t seem to show up any issues. But the more I think about it, I wonder if the whole above: '30' line is causing the issue? Is Numeric state the correct trigger type to be using? (Within the Developer tab, the State column is always “just a number”).

Try above: 30 without quotes.

Also, note it’ll only trigger when going from below to above 30. If you restart with power already above 30, it won’t trigger.

1 Like

Manually executing an automation skips the automation’s trigger and condition (if any) and simply runs its action. Therefore what you observed is nominal behavior.

Restarting Home Assistant will also reload automations (among many other things). However, if the goal is to simply reload automations then Configuration > Server Controls > Reload Automations is faster. However, I suggest you first run Configuration > Server Controls > Check Configuration to ensure whatever changes you made don’t contain errors.

NOTE
If you compose automation’s using the Automation Editor, there’s no need to run Check Configuration or to Reload Automations because it does that for you (when you save the automation).

No, it’s fine. The Numeric State Trigger will correctly handle the value whether it wrapped in quotes or not.

I believe the issue is what Troon has already explained. A Numeric State Trigger will only trigger at the moment the value crosses the threshold (in your case, the moment it rises above 30). If the value is already above 30, it will not trigger; it must first decrease below 30 and then rise above it.

According to your screenshot, the value is currently 141, well above the threshold, so it will not trigger until it drops below 30 and then rises above it. The moment it increases above 30, that’s when the Numeric State Trigger will trigger.

The same principle applies to the other automation except in the opposite direction because its Numeric State Trigger uses below for its threshold.

1 Like

Thank you both - very helpful. It is now working (I removed the quotes around the 30), and have set more appropriate trigger values.

Happy! :grinning:

1 Like