Offset with Sunset in a template

Just looking for a code check here. Trying to make a binary sensor that turns on as follows:

30 min from sunset & > 30% cloudy
or
sun elevation = -5

Does this look right?

- platform: template
  sensors:
    time_logic:
      value_template: >-
        "{{ states('sensor.owm_cloud_coverage')| float >= 30
                                  and states('sensor.time') == as_timestamp(state_attr('sun.sun', 'next_setting')) | timestamp_custom('%R')
                                  or  state_attr('sun.sun', 'elevation') | float <=-5}}"

seems a bit odd. It will only turn on for 1 minute.

Yea, this sensor will turn on with said criteria. I have lights that will turn on when this sensor turns on.

Well, if you have a sensor that turns on and off at the correct times… you can just make as simple automation:

- alias: lights with binary sensor
  trigger:
  - platform: state
    entity_id: binary_sensor.xxxxx
  action:
  - service: light.turn_{{ trigger.to_state.state }}
    entity_id: light.xxxxx

Thats the plan, thank you!

Well, I can help you tailor the template if you want to add an ending time to it. Just let me know your ideas.

@petro, I appreciate it, but what I really need is to confirm that the value template in my first post will do as I have listed. I new to writing templates and was more so looking for a peer review.

I have several lighting templates for different things and the turn on logic is redundant. I am going to a sensor to hold the turn on logic. Then in my lighting automation’s, when the sensor turns on, then turn on what is needed.

your template is fine, just remove the outer quotes because you’re using a multiline template