Automation not triggering for sun conditions?

Hi all, first post. I tried looking this up myself but couldn’t find anything specific that I’ve done wrong. The following two automations either work randomly, or flat out don’t trigger with the sun conditions. if I remove the condition of sun, they fire without a hitch. I am using MQTT as the trigger so as to make a failsafe (no missed event) automation, as the aquarium is a critical process. In my Vera hub, I used a plugin called Reactor to do this and it was pretty straightforward. I have used both the Automations UI and tried just typing the code in myself to no avail. It sometimes works and sometimes doesn’t. MQTT topic updates every 300 seconds and is showing, confirmed. Even if I force it (status in tasmota console) it still just randomly decides to work or not work.

https://pastebin.com/RnUrJpf0

- id: '1588720799099'
  alias: Aquarium UV On
  description: ''
  trigger:
  - platform: mqtt
    topic: aquarium/uvlamp/STATE
  condition:
  - condition: and
    conditions:
    - after: sunrise
      after_offset: -01:00:00
      before: sunset
      before_offset: +01:00:00
      condition: sun
    - condition: state
      entity_id: switch.uv_lamp
      state: 'off'
  action:
  - device_id: de96698250fd45b294e224738295affe
    domain: switch
    entity_id: switch.uv_lamp
    type: turn_on
- id: '1588721351891'
  alias: Aquarium UV Off
  description: ''
  trigger:
  - platform: mqtt
    topic: aquarium/uvlamp/STATE
  condition:
  - condition: and
    conditions:
    - after: sunset
      after_offset: +01:01:00
      before: sunrise
      before_offset: -01:01:00
      condition: sun
    - condition: device
      device_id: de96698250fd45b294e224738295affe
      domain: switch
      entity_id: switch.uv_lamp
      type: is_on
  action:
  - device_id: de96698250fd45b294e224738295affe
    domain: switch
    entity_id: switch.uv_lamp
    type: turn_off
    - after: sunrise
      after_offset: -01:00:00
      before: sunset
      before_offset: +01:00:00
      condition: sun

That’s not valid, those are two separate sun conditions, not one :wink:

    - after: sunrise
      after_offset: -01:00:00
      condition: sun
    - before: sunset
      before_offset: +01:00:00
      condition: sun

Yes, the UI editor will let you write invalid code :man_shrugging: The correct format is explained in the docs.

Personally I’d recommend using the sun elevation instead. That will have a consistent light level through the year.

1 Like

That appears to have done the trick! Here’s what it looks like in the UI.