Automation with sensor below value for X ammount of time

I need some help in converting this automation:

trigger:
  platform: numeric_state
  entity_id: sensor.fresi_power
  value_template: '{{ states.sensor.fresi_power.state }}'
  below: 1000
  for:
    seconds: 10
action:
  - service: switch.turn_off
    entity_id: switch.fresi

I’m not at my system so I can’t get you a flow but in theory it is like

  1. get the value from the sensor
  2. if the value is below your value
  • than put in a Delay ,
  • get the value from sensor again
  • if the sensor is still below your value than execute your action

I have tried that, but that doesn’t mean that’s been under that value the whole time

I don’t think it’s as easy as it seems to be. Power can keep going up and down during that 10 seconds and it seems like what you want is to only turn off the switch if power remains below 1000 for 10 seconds.

Here’s my take. I’m using a trigger to check if power is < 1000 for 10 seconds. If it becomes >= 1000 during the 10 seconds, it triggers the timer to reset. I have not run it so I’m not sure if it’s bug free. As with most programs, there can be several ways to do it and I might be complicating things. :wink:

[  
   {  
      "id":"fec74566.96bb68",
      "type":"tab",
      "label":"Fersi",
      "disabled":false,
      "info":""
   },
   {  
      "id":"470e5d3a.99835c",
      "type":"server-state-changed",
      "z":"fec74566.96bb68",
      "name":"Fersi Power",
      "server":"9b0e1469.366258",
      "entityidfilter":"sensor.fresi_power",
      "entityidfiltertype":"exact",
      "haltifstate":"",
      "x":70,
      "y":160,
      "wires":[  
         [  
            "6e66cc5c.870b64"
         ]
      ],
      "outputLabels":[  
         "states.sensor.fresi_power.state"
      ]
   },
   {  
      "id":"c2a84a4f.d16f48",
      "type":"trigger",
      "z":"fec74566.96bb68",
      "op1":"",
      "op2":"OFF",
      "op1type":"nul",
      "op2type":"str",
      "duration":"10",
      "extend":false,
      "units":"s",
      "reset":"ON",
      "bytopic":"all",
      "name":"",
      "x":430,
      "y":120,
      "wires":[  
         [  
            "b198ad3c.0521f"
         ]
      ]
   },
   {  
      "id":"6e66cc5c.870b64",
      "type":"switch",
      "z":"fec74566.96bb68",
      "name":"< 1000",
      "property":"payload",
      "propertyType":"msg",
      "rules":[  
         {  
            "t":"lt",
            "v":"1000",
            "vt":"num"
         },
         {  
            "t":"gte",
            "v":"1000",
            "vt":"num"
         }
      ],
      "checkall":"true",
      "repair":false,
      "outputs":2,
      "x":230,
      "y":160,
      "wires":[  
         [  
            "c2a84a4f.d16f48"
         ],
         [  
            "ebd13d76.282bf",
            "c2a84a4f.d16f48"
         ]
      ],
      "outputLabels":[  
         "OFF",
         "ON"
      ]
   },
   {  
      "id":"e5236dfc.90ac38",
      "type":"api-call-service",
      "z":"fec74566.96bb68",
      "name":"Switch Off",
      "server":"9b0e1469.366258",
      "service_domain":"switch",
      "service":"turn_off",
      "data":"{   \"entity_id\": \"switch.fresi\" }",
      "mergecontext":"",
      "x":890,
      "y":120,
      "wires":[  
         [  

         ]
      ]
   },
   {  
      "id":"ebd13d76.282bf",
      "type":"rbe",
      "z":"fec74566.96bb68",
      "name":"",
      "func":"rbe",
      "gap":"",
      "start":"",
      "inout":"out",
      "property":"payload",
      "x":590,
      "y":200,
      "wires":[  
         [  
            "8ff66e91.89a088"
         ]
      ]
   },
   {  
      "id":"8ff66e91.89a088",
      "type":"api-call-service",
      "z":"fec74566.96bb68",
      "name":"Switch On",
      "server":"9b0e1469.366258",
      "service_domain":"switch",
      "service":"turn_on",
      "data":"{   \"entity_id\": \"switch.fresi\" }",
      "mergecontext":"",
      "x":890,
      "y":200,
      "wires":[  
         [  

         ]
      ]
   },
   {  
      "id":"b198ad3c.0521f",
      "type":"rbe",
      "z":"fec74566.96bb68",
      "name":"",
      "func":"rbe",
      "gap":"",
      "start":"",
      "inout":"out",
      "property":"payload",
      "x":590,
      "y":120,
      "wires":[  
         [  
            "9e85430e.0c891"
         ]
      ]
   },
   {  
      "id":"9e85430e.0c891",
      "type":"switch",
      "z":"fec74566.96bb68",
      "name":"Off",
      "property":"payload",
      "propertyType":"msg",
      "rules":[  
         {  
            "t":"eq",
            "v":"OFF",
            "vt":"str"
         }
      ],
      "checkall":"true",
      "repair":false,
      "outputs":1,
      "x":730,
      "y":120,
      "wires":[  
         [  
            "e5236dfc.90ac38"
         ]
      ]
   }
]

I like your idea, there is a nice node called mytimeout which I use widely in my automation

  1. Set the timeout and start timer
  2. Whenever the sensor is above your value than reset the timer

It works a few times only, but not reliable. I was hoping for something more simple.

As you can see it doesn’t turn it off.

I did get it working as shown here.

[{"id":"7c3356d4.b3ebb8","type":"server-state-changed","z":"b0727dd7.cebbb","name":"Fresi W","server":"16490a7f.d39bb6","entityidfilter":"sensor.fresi_w","entityidfiltertype":"exact","haltifstate":"","x":110,"y":160,"wires":[["f055b7ee.463ff8"]],"outputLabels":["states.sensor.fresi_power.state"]},{"id":"f055b7ee.463ff8","type":"switch","z":"b0727dd7.cebbb","name":"Terminó la carga ?","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"1000","vt":"num"},{"t":"gte","v":"1000","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":310,"y":160,"wires":[["ec1e1184.a63f18"],["e89ee659.cc471"]],"outputLabels":["Si","No"]},{"id":"ec1e1184.a63f18","type":"trigger","z":"b0727dd7.cebbb","op1":"","op2":"Off","op1type":"nul","op2type":"str","duration":"10","extend":false,"units":"s","reset":"On","bytopic":"all","name":"","x":510,"y":80,"wires":[["b6426d26.01339","eadd878b.e984e8"]]},{"id":"e89ee659.cc471","type":"change","z":"b0727dd7.cebbb","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"On","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":220,"wires":[["ec1e1184.a63f18"]]},{"id":"b6426d26.01339","type":"api-call-service","z":"b0727dd7.cebbb","name":"Switch Off","server":"16490a7f.d39bb6","service_domain":"switch","service":"turn_off","data":"{   \"entity_id\": \"switch.fresi\" }","mergecontext":"","x":690,"y":80,"wires":[[]]},{"id":"eadd878b.e984e8","type":"api-call-service","z":"b0727dd7.cebbb","name":"Notificación de Telegram","server":"16490a7f.d39bb6","service_domain":"telegram_bot","service":"send_message","data":"{ \"message\" : \"Fresi se ha terminado de cargar\" }","mergecontext":"","x":760,"y":140,"wires":[[]]},{"id":"16490a7f.d39bb6","type":"server","z":"","name":"Hassio","url":"https://XXXXXX:8123","pass":"XXX"}]

You might be better off using either a Statistics Sensor or Filter Sensor in HA to smooth the data before you do the check in node-red. This will help remove fluctuations in the data.

i did this type of thing to set a timer for my lights to turn of after motion was detected. And to reset that timer when new motion was detected again.
What i did was to create a timer in HA,
then in node-red one trigger when motion was detected(for you fresi w below 1000) -> start timer. (start timer resets the time in HA if its already running)

then a seperate flow, with a trigger when timer reaches 0 -> turn off lights(for you switch.turn_off)

this way it will trigger and reset the timer everytime it dips below 1000, and if it doesnt happen when the timer is running, once down to 0 till will turn off the switch.

(Im at work so cant show the flows)

I did solved it with only 1 flow.
I use a switch with 2 outputs: more than 1000W and less than 1000W
Using a trigger to send nothing and after X seconds sends “off” to switch off the motorbike charger
and in parallel a change node that changes the payload to “reset” and to the trigger