Help with basic automation for sunset

Hi Guy,

First apologies, as this is reposted from elsewhere

I have a very simple automation that I’m trying to convert from yamal to node-RED, which switches my porch light on when I arrive home after sunset and then switches it off after 5 minutes. I’m struggling to setup in a similar way in node-RED. I’ve tried using big timer, which seems like overkill and was hoping to use the delay to switch off after 5 minutes. Is it possible to use the HA sun state in node-red to set the condition? I am very new to node-red, so apologies for my ignorance, as I’m sure this is a simple automation to achieve.

alias: Porch light on
  trigger:
  - entity_id: group.tracker_matt
    from: not_home
    platform: state
    to: home
  condition:
  - after: sunset
    condition: sun
  action:
  - data:
      entity_id: light.hue_white_light_1_2
    service: light.turn_on
- id: '1543501274770'
  alias: Porch Off
  trigger:
  - entity_id: light.hue_white_light_1_2
    for: 0:05
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - data:
      entity_id: light.hue_white_light_1_2
    service: light.turn_off

35

You’re better off using the event-state function for this case. The below example works basically the same as your yaml automation.

Triggers on group.tracker_matt being to set to home then checks the condition of sun and turns on light delays 5 mins and then turns off light.

[{"id":"5d7aa38b.2e720c","type":"time-range-switch","z":"1a9590f5.2d236f","name":"","lat":"","lon":"","startTime":"sunset","endTime":"sunrise","startOffset":"","endOffset":0,"x":400,"y":1088,"wires":[["b49e6fd.7a0a89","359368fe.e4e1a8"],[]]},{"id":"b49e6fd.7a0a89","type":"api-call-service","z":"1a9590f5.2d236f","name":"Porch On","server":"2dad33ee.42bf5c","service_domain":"light","service":"turn_on","data":"{\"entity_id\":\"light.hue_white_light_1_2\"}","render_data":false,"mergecontext":"","x":588,"y":1040,"wires":[[]]},{"id":"359368fe.e4e1a8","type":"delay","z":"1a9590f5.2d236f","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":588,"y":1088,"wires":[["59b5c6e4.9e5418"]]},{"id":"caed1797.1b5c08","type":"server-state-changed","z":"1a9590f5.2d236f","name":"Matt Home","server":"2dad33ee.42bf5c","entityidfilter":"group.tracker_matt","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"home","halt_if_type":"str","halt_if_compare":"is_not","x":236,"y":1088,"wires":[["5d7aa38b.2e720c"]]},{"id":"59b5c6e4.9e5418","type":"api-call-service","z":"1a9590f5.2d236f","name":"Porch Off","server":"2dad33ee.42bf5c","service_domain":"light","service":"turn_off","data":"{\"entity_id\":\"light.hue_white_light_1_2\"}","render_data":false,"mergecontext":"","x":732,"y":1088,"wires":[[]]}]
1 Like

Thanks Kermit, this is great, thanks I’m still slowly learning to use node-red, but so far I’m liking the more visual way of creating an automation. I’ll try this tonight when I get home from work.

Which node did you use for the sunset? it seems like its not simple to use the sun entity from HA

Sorry, forgot to add that.

node-red-contrib-time-range-switch

You can most definitely use the sun entity from HA but I prefer to use the node-red-contrib-time-range-switch node for its flexibility within Node-RED.

[{"id":"2dbdbeb0.8aa272","type":"api-call-service","z":"caad1953.456cd8","name":"Porch On","server":"2dad33ee.42bf5c","service_domain":"light","service":"turn_on","data":"{\"entity_id\":\"light.hue_white_light_1_2\"}","render_data":false,"mergecontext":"","x":700,"y":1296,"wires":[[]]},{"id":"d725de4a.49c25","type":"delay","z":"caad1953.456cd8","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":700,"y":1344,"wires":[["b39b33c.7eeb3d"]]},{"id":"81b4a725.147a78","type":"server-state-changed","z":"caad1953.456cd8","name":"Matt Home","server":"2dad33ee.42bf5c","entityidfilter":"group.tracker_matt","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"home","halt_if_type":"str","halt_if_compare":"is_not","x":316,"y":1344,"wires":[["6e8c91c3.13557"]]},{"id":"b39b33c.7eeb3d","type":"api-call-service","z":"caad1953.456cd8","name":"Porch Off","server":"2dad33ee.42bf5c","service_domain":"light","service":"turn_off","data":"{\"entity_id\":\"light.hue_white_light_1_2\"}","render_data":false,"mergecontext":"","x":844,"y":1344,"wires":[[]]},{"id":"6e8c91c3.13557","type":"api-current-state","z":"caad1953.456cd8","name":"","server":"2dad33ee.42bf5c","halt_if":"below_horizon","halt_if_type":"str","halt_if_compare":"is_not","override_topic":true,"override_payload":true,"override_data":true,"entity_id":"sun.sun","state_type":"str","x":516,"y":1344,"wires":[["d725de4a.49c25","2dbdbeb0.8aa272"]]}]
1 Like

Great thanks I’ll check these both one.

@Kermit I’ve just had a look at the automations you put together for me and just have one question. I noticed that you set the event state node for the presence group to halt if home. This has confused me, as my assumption is that the automation won’t run, because when I arrive home the state of the group will change to home. Is that correct or am I missing something? I’m trying to get the automation to run when I arrive home. But maybe I misunderstand this?

What version of the node-red home assistant module are you running? node-red-contrib-home-assistant or node-red-contrib-home-assistant-websocket? If you running the first one I would recommend switching to the websocket one, full disclosure I maintain the websocket one.

On the websocket version, the halt if logic with actually be is not home so it works out. You are correct if you’re using node-red-contrib-home-assistant because it doesn’t have the new logic comparators. It would need to be changed to not_home.

Phew glad to know my understanding was correct. Although node-RED says I’m running the websocket one. I just installed the node-RED addon in hassio and haven’t made any other changes. Although I’ve just noticed there is an update for the websocket version. I’ve just tried updating and its saying

Failed to update: node-red-contrib-home-assistant-websocket

Module not locally installed

Check the log for more information

Yeah, if you’re using hass.io you’ll have to wait until the addon is updated to get the latest features. You can just change the halt if logic to not_home and it should function the same.

OK thanks for letting me know