Automation turn on at sunset and off at time

I’ve been using multiple automations for turning on and off a light. But lately I’ve been experimenting with templates and thus been able to remove the OFF automation by combining it into one and the same.

However, when I want to trigger on both sun and time, I’ve run into some issues, this is what my YAML says:

alias: Lavalamp
description: Turn on/off lavalamp based on sun and 10pm
trigger:
  - platform: state
    entity_id: sun.sun
  - platform: time
    at: '22:00'
condition:
  - condition: template
    value_template: '{{ trigger.to_state.state != trigger.from_state.state }}'
action:
  - service: >-
      switch.turn_{{ 'on' if trigger.to_state and trigger.to_state.state ==
      'below_horizon' else 'off' }}
    target:
      entity_id: light.office_moddim_lavalamp
mode: single

This doesn’t work at all. And I’m stumped looking for any kind of help for this. I would love to be able to do debugging on my automation and also dump the contents of the trigger object so I know what’s going on here, but can’t find any info on that either.

Any suggestions?

The automation shall turn on my lavalamp at sundown and turn it off again at 10pm (in case my YAML is that bad ;-))

1 Like

I don’t really have a good way to test it but I think it should work like this:

alias: Lavalamp
description: Turn on/off lavalamp based on sun and 10pm
trigger:
  - platform: sun
    event: 'sunset'
  - platform: time
    at: '22:00'
action:
  - service: >-
      switch.turn_{{ 'on' if trigger.platform == 'sun' else 'off' }}
    target:
      entity_id: light.office_moddim_lavalamp
mode: single

there is a doc page giving all the available data for the trigger object:

2 Likes

Let us know if you get this working. I’d like to do the same thing!