Simple off automation using numeric state of sensor does not work (on version does)

Hi all,

Running home assistant on VM (esxi) for over a year now:
Version - core-2021.7.3
Supervisor - supervisor-2021.06.8
Installation Type - Home Assistant OS

I have an issue with a very simple automation.

My goal:

  • to start a pool heater when the electrical power i put back on the net goes above a certain treshold
  • to stop the pool heater when the electrical powder i put back on the net goes below a certain treshold

for this i have made two automations via the GUI (on for the on part and one for the off part):

on automation:

- id: '1625839064014'
  alias: verwarming zwembad aan
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.elec_terug_watts
    above: '3100'
    for: 0:05:00
  condition:
  - condition: device
    type: is_off
    device_id: 67f8ed2b26731c31ab9b9cbd783d945d
    entity_id: switch.carportzwembadverwarming
    domain: switch
    for:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  action:
  - type: turn_on
    device_id: 67f8ed2b26731c31ab9b9cbd783d945d
    entity_id: switch.carportzwembadverwarming
    domain: switch
  mode: single

off automation:

- id: '1626471103514'
  alias: zwembad uit test
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.elec_terug_watts
    below: '100'
  condition: []
  action:
  - type: turn_off
    device_id: 67f8ed2b26731c31ab9b9cbd783d945d
    entity_id: switch.carportzwembadverwarming
    domain: switch
  mode: single

The on automation behaves perfectly, the off automation does not. If i manually run the automation, the action gets done (setting of the heater), so this part at least works.

the sensor_elec_terug_watts is just basic sensor, changing the input i get from my utility meter from Kw to Watt:

    sensors:
     elec_terug_watts:
        friendly_name: "electriciteitterug in Watt"
        unit_of_measurement: "W"
        value_template: "{{ states('sensor.electriciteitterug') | float * 1000 }}"

The switch.carportzwembadverwarming is just a tasmota-flashed shelly 1 PM

As this is really a very straight forward thing to do, i’m really insure what I am doing wrong, so any help would be appreciated!

Many thanks and kind regards,

Ben

Have you read about how numeric_state trigger works?
Fires when the numeric value of an entity’s state (or attribute’s value if using the attribute property) crosses a given threshold.

You can also read about numeric_state trigger here - a blog post made by Tinkerer.

So, the off automation only triggered if the value of sensor.elec_terug_watts start above your threshold and then go under your threshold.

Note: When you manually run the automation, it skips directly to the action part of your automation.

1 Like

If you want HA to set the heater on/off when you just missed the moment the threshold was passed (for example a HA restart) you can add HA restart as a trigger. If you then also add everything to a single automation you end up with

alias: Verwarming zwembad
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.elec_terug_watts
    above: '3100'
    for: '0:05:00'
  - platform: numeric_state
    entity_id: sensor.elec_terug_watts
    below: '100'
  - platform: homeassistant
    event: start
condition: []
action:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.elec_terug_watts
            above: '3100'
        sequence:
          - type: turn_on
            device_id: 67f8ed2b26731c31ab9b9cbd783d945d
            entity_id: switch.carportzwembadverwarming
            domain: switch
    default:
      - type: turn_off
        device_id: 67f8ed2b26731c31ab9b9cbd783d945d
        entity_id: switch.carportzwembadverwarming
        domain: switch
mode: single

Thanks for this, i will probably incorporate this into my automations (i have some more).

Hi,

Thanks for your reply. I have in fact read up on the numeric_state and thought it was quite self-explanatory. Sure i grasp the concept of the tresholds, however i have tried these automations numerous days (i.e. the tresholds were met multiple times) and only the on automation fired away.

Perhaps let me explain what I see in home assistant during the day.

  • the sun starts shining in the morning, and after a while the ‘sensor.elec_terug_watts’ starts climbing from 0 up to ~8700 at the peak of the day.
  • when it has passed 3100 for at least 5 minutes (the sensor gets an update each second) the automation for on starts (as it should be)
  • in the afternoon-night (or when multiple heavy consumers in my house are running) this ‘sensor.elec_terug_watts’ will return to 0.
  • when it has passed back the treshold (i.e. below 100), the off automation does not fire.

As I don’t have any conditions in my off automation, is there anything I am missing? Does the situation described above, not meet everything for the automation to fire?

Many thanks!

Ben

Hello! Can you take a look at Troubleshooting Automations?

What did you find out when using debug traces for your off automation?

The first thing to do is troubleshooting as ardysusilo mentioned above.

My guess would be, that sensor.electriciteitterug isn’t updated regularly. Can you check the history of that sensor? Is it really working the way it should (eg. passing the 100 treshold)?

Hi!

Well I definitely had a look there before, problem is, my automations never gets triggered, hence there is no trail to debug…

debug of the on automation is quite normal i think…

trigger:
  id: '0'
  idx: '0'
  platform: numeric_state
  entity_id: sensor.elec_terug_watts
  below: null
  above: 3100
  from_state:
    entity_id: sensor.elec_terug_watts
    state: '3025.0'
    attributes:
      unit_of_measurement: W
      friendly_name: electriciteitterug in Watt
    last_changed: '2021-07-22T12:25:47.260229+00:00'
    last_updated: '2021-07-22T12:25:47.260229+00:00'
    context:
      id: 09a347cfe55c513fa447a4c232b6aa56
      parent_id: null
      user_id: null
  to_state:
    entity_id: sensor.elec_terug_watts
    state: '3125.0'
    attributes:
      unit_of_measurement: W
      friendly_name: electriciteitterug in Watt
    last_changed: '2021-07-22T12:25:49.278094+00:00'
    last_updated: '2021-07-22T12:25:49.278094+00:00'
    context:
      id: 11f0187e12b926e6716b21785e934c53
      parent_id: null
      user_id: null
  for:
    __type: <class 'datetime.timedelta'>
    total_seconds: 300
  description: numeric state of sensor.elec_terug_watts

You can try changing the value of sensor.elec_terug_watts in-
Developer Tools → States (tab) → Type sensor.elec_terug_watts in the entity → Put a random number under 100, for example 50 in the stateSet State

After that, please check again at your off automation debug.

Also, make sure to check the toggle button for your off automation. Sometimes we forget that we turn off the automation.

Thanks Patrick,

if that’s true, wouldn’t the same issue occur with the on automation? The logic behind the two automation-triggers (i.e. above xxxx and below xxxx) is the same, no?

the sensor should update more or less each second, this works via a digital read-out directly from my utility meter (there is a conversion step from KW to Watt, as listed above), i dont know the implications of that on the refresh rate, however i see the values changing on my home dashboard quite rapidly (also more or less each scond).

screenshot of the history of that sensor:

Capture

kind regards,

Ben

Did you make the automation in the UI? If not, did you reload the Automations? Is it visible in the UI?

Otherwise, maybe delete the automation and try to build it again. Because we might be blind but I see nothing wrong.

And in the graph, the resolution is a bit coarse, does it really drop below 100 in that part?

PS Damn, 8kW, I really hope your inverter is 3-fase :smiley:

This is just a guess, really, so don’t take it as a solution right away! :wink:

Two things come to mind:

  • to much stress on HA and it’s automations. Meaning the template sensor to change from W to kW isn’t fast enough to keep up. Just for the sake of trying, can you leave that one out and change the automation to work with W?
  • The sensor is updated to frequently, especially with some spikes, so the “turn_off” get’s changed to “turn_on” again within a split second.

Hi Timo,

yes the automation was made in the UI, and is visible in the UI.

yup, it dropped below 100 half an hour, when my heatpump started drawing power at 1PM for hot water (it eats a lot of power).

And yup, i have ~12.5 KW worth of solar panels on my roof, with a 8.7 KW converter on that. This was cmbined with a ~15KW heat pump (drawing peak power of around 5KW). And after ejoying this system for 1 year, on of the Belgian governments (Flemish) destroyed the previous agreement of basically using the power grid as a battery (your yearly solar production is set 1:1 with your yearly production). Hence i am looking now at battery systems and more complex automations (this one is just the start) to optimize my energy.

thanks!

Ben

Thank you,

I just tried this and for the first time ever, the automation fired, hence the automation should work.

hmm, do you know the ‘refresh rate’ at which home assistant checks the trigger conditions? perhaps this is too low/high in comparison with the refresh rate of the sensor?

thank you!

KR,
Ben

Thanks Patrick,

Indeed a lot of calculations, but it seems to me HA is quite responsive to this (VM has two old Xeon-cores working for him - Xeon 2680v2) + decicated SSD + 4GB of RM.

If it were true on the sensor update frequency, would the log of the automation not register this?

thank you!

KR,

Ben

It should be registered in the automation debug log once the state becomes lower than your threshold.

The fact that changing it via Developer Tools works making it an even bigger mystery :confused:

Indeed a mystery. When en entity updates all automations that have that entity as a trigger are checked, there is no polling.

Is it possible to enable a high load? That way, if you remake the automation with a bit higher below threshold you can actually try the automation right away (instead of waiting for the sun to drop / water heater to heat.

Hi all,

I don’t know how to explain this, but suddenly the automation off seems to be working. As there was just a dip in solar power, the off automation fired… really flabbergasted now this is suddenly working… anyway, hopefully it stays this way… thanks for all the help and suggestions, i have again learned some new stuff while troubleshooting this!

ps the log of the off automation:

trigger:
  id: '0'
  idx: '0'
  platform: numeric_state
  entity_id: sensor.elec_terug_watts
  below: 100
  above: null
  from_state:
    entity_id: sensor.elec_terug_watts
    state: '309.0'
    attributes:
      unit_of_measurement: W
      friendly_name: electriciteitterug in Watt
    last_changed: '2021-07-22T13:57:38.920190+00:00'
    last_updated: '2021-07-22T13:57:38.920190+00:00'
    context:
      id: 51a490d2fd564f5195f780bdfce0767c
      parent_id: null
      user_id: null
  to_state:
    entity_id: sensor.elec_terug_watts
    state: '34.0'
    attributes:
      unit_of_measurement: W
      friendly_name: electriciteitterug in Watt
    last_changed: '2021-07-22T13:57:40.934287+00:00'
    last_updated: '2021-07-22T13:57:40.934287+00:00'
    context:
      id: 3a94d90136a4cb50781a72c4f736c9f4
      parent_id: null
      user_id: null
  for:
    __type: <class 'datetime.timedelta'>
    total_seconds: 300
  description: numeric state of sensor.elec_terug_watts

kind regards,

Ben

Glad it worked out in the end :slight_smile:

Great it’s working! :+1:

Nonetheless, something seems off. 300 seconds to run this automation? The state change is from 309 to 34…seems a little “late” to the under-100-party, doesn’t it? :slight_smile: I’d monitor this sensor over the next days and would watch out for state changes and how everything reacts. As I said, I get a feeling, something is not 100% right. :slight_smile: