NodeRed - Calculate the minimum of two times?

Hey folks, in NodeRed I want to trigger an action at 5 AM as long as that is before sunrise. If it is not before sunrise, then don’t trigger it.

One way I can see to do it is to set a variable each morning at sunrise, then check the variable at 5AM…but is there a better way?

Use the time range node, be sure to enter your lat and lon.

image

Set it to sunset to sunrise and use the within output.

image

or a current state

image

Event: state node, with a tiny bit of JSONata and the time-date integration using sensor.time

$entity().state="05:00" and $entities('sun.sun').state="below_horizon"

As long as you have sensor.time to provide local time as “hh:mm”, the node state condition will only be true when time is 5AM and it is before sunrise.

[{"id":"5b8cf2ba61949b6e","type":"server-state-changed","z":"e6f8ea0317d4f26a","name":"5AM before sunrise","server":"","version":5,"outputs":2,"exposeAsEntityConfig":"","entityId":"sensor.time","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"$entity().state=\"05:00\" and $entities('sun.sun').state=\"below_horizon\"","ifStateType":"jsonata","ifStateOperator":"jsonata","outputOnlyOnStateChange":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"x":430,"y":1880,"wires":[["8238d0e4229e9849"],["a0c9515da9b5af71"]]}]

Better? perhaps…

Thanks! Does sensor.time get created by default?

No - the sun.sun and sensor.sun_* does, but you need to add ‘time’ integration by hand.

I have added mine using the YAML configuration settings, which still work, but I note that there is now a ‘time-date’ integration, and you have to add this and create an entity for each ‘time/date’ you want. I find the ‘time’ very useful, as it updates every minute and being local deals with timezone and DST. The UTC and ISO times are also very useful (as most parsing only likes ISO formatted timestamps).

If it is any help, I have the following in my config YAML and this works a treat, but if you are starting from scratch, use the time-date integration.

sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_utc'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'

Amazing - thanks all.