Automations with Trigger and Condition

Hey all,
I’m looking for an automation-solution with a specific requirement.
Based on having a specific PV surplus (trigger) for a certain period and under the condition of being below a specific room temperature (condition), a socket should be activated (action).
My problem is that if the condition is not met at the time of trigger activation but is met later, the automation does not trigger again (missing trigger).
Creating the second automation with temperature as trigger and PV surplus as condition is not the same, as I cannot set a time period as condition for the PV surplus.
Is there any easy solution for this. I’m coming from ioBroker and implemented this behavior there in Blockly.

I have a workaround in mind, but it requires kind of variables or helpers which I would like to avoid.

Btw: Originally I planned to have one “Turn on” Automation and two “Turn off” automations (one temperature based, one PV surplus based).

Thanks
Stefan

Share the code you have right now and you’ll have a much better chance that somebody will actually be able to help you :wink:

Normally I’d use both states as a trigger and a condition, but your wording is too vague to provide a real approach to your issue.

Here ou go:

var timeoutHeizluefterEinActive, timeoutHeizluefterAusActive, grenzeAusschaltenWatt, grenzeEinschaltenWatt, grenzeTemperatur, eingeschaltetDurchScript, timeoutHeizluefterEin, timeoutHeizluefterAus;


timeoutHeizluefterEinActive = false;
timeoutHeizluefterAusActive = false;
grenzeAusschaltenWatt = 200;
grenzeEinschaltenWatt = -500;
grenzeTemperatur = 26.5;
eingeschaltetDurchScript = false;
on({ id: [].concat(['sonoff.0.Hitchi.SML_16_7_0']).concat(['openknx.0._DG-Bad_-_Temperatur_-_TEMP_']), change: 'ne' }, async (obj) => {
  console.debug(('Aktuelle Stromerzeugung: ' + String(getState('sonoff.0.Hitchi.SML_16_7_0').val)));
  console.debug(('Status Heizlüfter: ' + String(getState('hue.0.Heizlüfter_Plug.on').val)));
  if ((parseFloat(getState('sonoff.0.Hitchi.SML_16_7_0').val) < grenzeEinschaltenWatt) && (parseFloat(getState('openknx.0._DG-Bad_-_Temperatur_-_TEMP_').val) < grenzeTemperatur) && (getState('hue.0.Heizlüfter_Plug.on').val == false) && (timeoutHeizluefterEinActive == false)) {
    // Genügend Stromüberschuss & Heizlüfter aus & kein Timeout zum Einschalten aktiv
    console.debug(('Timer Heizlüfter "Ein" starten: ' + String(getState('sonoff.0.Hitchi.SML_16_7_0').val)));
    timeoutHeizluefterEinActive = true;
    timeoutHeizluefterEin = setTimeout(async () => {
      timeoutHeizluefterEin = null;
      console.debug('Heizlüfter einschalten');
      timeoutHeizluefterEinActive = false;
      setState('hue.0.Heizlüfter_Plug.on' /* Heizlüfter Plug.on */, true);
      eingeschaltetDurchScript = true;
    }, 180000);
  } else if ((parseFloat(getState('sonoff.0.Hitchi.SML_16_7_0').val) < grenzeAusschaltenWatt) && (parseFloat(getState('openknx.0._DG-Bad_-_Temperatur_-_TEMP_').val) < grenzeTemperatur) && (getState('hue.0.Heizlüfter_Plug.on').val == true) && (timeoutHeizluefterAusActive == true)) {
    // Heizlüfter läuft und Timeout für Ausschalten aktiv
    console.debug(('Timer Heizlüfter "Aus" stoppen: ' + String(getState('sonoff.0.Hitchi.SML_16_7_0').val)));
    timeoutHeizluefterAusActive = false;
    (() => { if (timeoutHeizluefterAus) { clearTimeout(timeoutHeizluefterAus); timeoutHeizluefterAus = null; }})();
  } else if (((parseFloat(getState('sonoff.0.Hitchi.SML_16_7_0').val) >= grenzeAusschaltenWatt) || (parseFloat(getState('openknx.0._DG-Bad_-_Temperatur_-_TEMP_').val) >= grenzeTemperatur)) && (getState('hue.0.Heizlüfter_Plug.on').val == true) && (timeoutHeizluefterAusActive == false) && (eingeschaltetDurchScript == true)) {
    // Nicht genügend Stromüberschuss & Heizlüfter an & kein Timeout für Ausschalten aktiv
    console.debug(('Timer Heizlüfter "Aus" starten: ' + String(getState('sonoff.0.Hitchi.SML_16_7_0').val)));
    timeoutHeizluefterAusActive = true;
    timeoutHeizluefterAus = setTimeout(async () => {
      timeoutHeizluefterAus = null;
      console.debug('Heizlüfter ausschalten');
      timeoutHeizluefterAusActive = false;
      setState('hue.0.Heizlüfter_Plug.on' /* Heizlüfter Plug.on */, false);
      eingeschaltetDurchScript = false;
    }, 180000);
  } else if (((parseFloat(getState('sonoff.0.Hitchi.SML_16_7_0').val) >= grenzeEinschaltenWatt) || (parseFloat(getState('openknx.0._DG-Bad_-_Temperatur_-_TEMP_').val) >= grenzeTemperatur)) && (getState('hue.0.Heizlüfter_Plug.on').val == false) && (timeoutHeizluefterEinActive == true)) {
    // Heizlüfter aus und Timeout für Einschalten aktiv
    console.debug(('Timer Heizlüfter "Ein" stoppen: ' + String(getState('sonoff.0.Hitchi.SML_16_7_0').val)));
    timeoutHeizluefterEinActive = false;
    (() => { if (timeoutHeizluefterEin) { clearTimeout(timeoutHeizluefterEin); timeoutHeizluefterEin = null; }})();
  }
});

Really not sure if it helps, it’s a lot and in HomeAssistant it seems to be much shorter. This “eingeschaltetDurchScript” can be ignored right now.

Hmm, both as trigger and also both as condition then would also work I assume. If I’m thinking about it… But I’m open for enhancements :wink:

OK, that’s what it looks like now

alias: Einschalten Leistung
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.scb_grid_power
    for:
      hours: 0
      minutes: 2
      seconds: 0
    below: -600
    enabled: true
  - trigger: numeric_state
    entity_id:
      - sensor.temperatur_bad
    below: 26.5
conditions:
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.scb_grid_power
        below: 600
      - condition: numeric_state
        entity_id: sensor.temperatur_bad
        below: 26.5
        enabled: true
actions:
  - type: turn_on
    device_id: 6a308a24d0fe9e3a72d14556660937a9
    entity_id: f62c699fdff755616f1cc70e530ad9ec
    domain: switch
    enabled: true
mode: single

Two things - not sure, if either of them will resolve the issue, but let’s take it step by step:

  1. The trigger for sensor.scb_grid_power shows below: -600 while the condition for the same sensor shows below: 600 - so there’s either a ‘-’ missing or one too many.

  2. Conditions are ‘AND’ by default, so line 2 and 3 in the conditions section are superfluous at best, maybe also causing an issue (not sure about it, though):

conditions:
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.scb_grid_power
        below: 600
      - condition: numeric_state
        entity_id: sensor.temperatur_bad
        below: 26.5

Try this instead:

conditions:
  - condition: numeric_state
    entity_id: sensor.scb_grid_power
    below: 600
  - condition: numeric_state
    entity_id: sensor.temperatur_bad
    below: 26.5
1 Like

Thanks, 600 is indeed an error. I was to fast yesterday evening. - means that energy is feeded into the grid. And also thanks for the hint with the hint. I will make it as easy as possible.

You should be able to use the same for parameter on your state condition as well. If you don’t, the automation will trigger on the change of temperatur_bad without taking into account how long scb_power_grid has held the correct value.

Also do replace your device action at the end with a switch.turn_on action. Device actions are easy to setup in the visual editor, but impossible to understand which devices/entities they act on by looking at the code.

alias: Einschalten Leistung
triggers:
  - trigger: numeric_state
    entity_id: sensor.scb_grid_power
    below: -600
    for:
      minutes: 2
  - trigger: numeric_state
    entity_id: sensor.temperatur_bad
    below: 26.5
conditions:
  - condition: numeric_state
    entity_id: sensor.scb_grid_power
    below: -600
    for:
      minutes: 2
  - condition: numeric_state
    entity_id: sensor.temperatur_bad
    below: 26.5
actions:
  - type: turn_on
    device_id: 6a308a24d0fe9e3a72d14556660937a9
    entity_id: f62c699fdff755616f1cc70e530ad9ec
    domain: switch

Thanks for the idea. Unfortunately the “for” is not allowed for numeric_states.
In the meantime my conclusion is: The triggers in combination with conditions are wrong. They are just not correctly implemented in HA. A trigger is always related to its condition. I tried to create the conditions also as trigger and vice versa. But if you come to the timer-dependency, it’s simply not the same anymore.