Trigger with Time Latch / Hysteresis. If Solar Export > X Switch On Y and Review after Z Mins

Hi All,

I’m quite new to HomeAssistant so be gentle :slight_smile:

I have sensors for my Solar Production including current import power, consumption power and export power etc. What I would like to do is the following. Some sudo code to hopefully describe the flow:

Latch = false.
Loop Every Minute:
    If current time is '> sunrise' AND '< sunset' AND 'Latch = false':
        If 'sensor.export_power' is greater than X Wh:
            Switch and latch on 'switch.my_plug' for Y minutes. Latch = true.
        Else:
            Latch = false.
        End If
    Else:
        Either Night Time or already latched on.
End If

So if ever the export power is above X it will switch on ‘my_plug’ for at least Y minutes. This is so the switch is not continually switching on and off. Ideally the check would run every minute so if export does rise above a certain limit it kicks into life.

An alternative maybe is that the automation runs every Y minutes and if export is above X then switch on the plug else leave it off. The downside of this is that you could miss the opportunity to turn it on sooner.

Interested to know peoples ideas and solutions for such a problem. I’m sure someone has solved it before but as yet I haven’t found and example I can copy.

Thanks in Advance,

Any ideas anyone??

I’d do this thus:

Automation 1: trigger on export power above threshold, condition that sun is up, switch on
Automation 2: trigger on either sun down or switch being on for Y minutes, switch off

Does that achieve what you want? If so, we can have a look at putting an automation together. It’s not entirely clear from your last else statement what you want to happen when the sun goes down while the switch is on and Y minutes is not yet up.

“Pseudo-code”. Sudo code is different.

Hi @Troon. That looks exactly what I’m after. :+1:

If the sun goes down then it should cancel the timer ideally. I.e. Before it gets to Y minutes.

Thanks in Advance,

Something like the below should work for you. This is a starting point for your experimentation, not a guaranteed-to-work solution that you can simply copy and paste, then come back if it doesn’t work exactly as you intended (although happy to help with problems & questions). There is plenty of excellent documentation on automations to help you.

There are a number of ways to solve this — here is mine (obviously, substitute X and Y for the real numbers):

# first automation
alias: Solar - plug on
description: Switch plug on when export rises above X when the sun is up
id: b6d96c15-dc35-40d9-afd5-27d519378537
trigger:
  - platform: numeric_state
    entity_id: sensor.export_power
    above: X

condition:
  - condition: state
    entity_id: sun.sun
    state: "above_horizon"

action:
  - service: switch.turn_on
    entity_id: switch.my_plug

# second automation
alias: Solar - plug off
description: Switch plug off after Y mins or at sunset
id: 07f252da-359e-4f44-952c-ba9b92b472b3
trigger:
  - platform: state
    entity_id: switch.my_plug
    to: 'on'
    for:
      minutes: Y
  - platform: state
    entity_id: sun.sun
    state: "below_horizon"

action:
  - service: switch.turn_off
    entity_id: switch.my_plug

Note that if the sensor is already above X at sunrise, this will not trigger — but I assume that’s not likely?

This shows how I currently set out my automations: the alias will get turned into the entity id, like automation.solar_plug_on and allows categorisation (the “Solar -” prefix will group related automations together in the UI), and the id is needed for the automation debugger to work on automations not created via the UI, and allows you to assign the automation to an area. I just use UUID4 IDs for this field via this tool.

1 Like

Thanks @Troon ! Sorry for the delayed thanks!

1 Like