Getting my head around sensor and trigger formatting

Hi. I’ve tried to look this up and im sure it’s simple but i’ve confused myself so thought I would try asking.

Basically, I have a motion sensor. I only want to actually trigger lights at night, so I thought it would be best on my esphome device which the pir sensor is attached to, to use the sunrise option, then only if sunset (with an offset) actually shows as motion.

substitutions:
  devicename: 53-motion-front-lights
  friendly_name: Motion Lights
  lower_devicename: motion_front_lights
  upper_devicename: Motion_Front_Lights

esphome:
  name: $devicename

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: 

ota:
  password: 

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Optional manual IP
  manual_ip:
    static_ip: 
    gateway: 
subnet: 255.255.255.0

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "$friendly_name Fallback Hotspot"
    password:

captive_portal:

time:
  - platform: homeassistant
    id: homeassistant_time
#time:
#  - platform: sntp
#    id: sntp_time

sun:
  latitude: !secret latitude_data
  longitude: !secret longitude_data
  id: sun_sun

binary_sensor:
  - platform: gpio
    pin:
      number: D1
      inverted: true
      mode:
        input: true
        pullup: true
    name: "Motion"
    device_class: Motion
    id: $lower_devicename
    on_state:
      then: 
      - if:
          condition:
            and: 
              - sun.is_above_horizon
          then:
            - logger.log: "Sun is above horizon - no trigger"
          else:
            - logger.log: "Below horizon go for it"

I’ve included my current yaml file. Im aware that it doesn’t actually ever change the motion detection of the sensor yet.

I thought that if I detected motion but it was still sunrise I could get it to return as clear (no motion detection) but if it’s below horizon and motion detected I could have the detected output.

Please can someone give me some guidance? I’ve tried to go through the triggers esphome page and haven’t been able to quite piece it together.

many thanks.

try the condition without the and:

      - if:
          condition:
            - sun.is_above_horizon
          then:
            - logger.log: "Sun is above horizon - no trigger"
          else:
            - logger.log: "Below horizon go for it"

Thanks, i think there is a bit more to it than just that though. I’ve removed the and its seemingly made no difference. I actually want to suppress the state so that only during nighttime will it ever give “detected” on the motion. I guess by checking the on_state point it’s already changed state by that time which is no good to me.

I see what you mean, will this work?

substitutions:
  devicename: 53-motion-front-lights
  friendly_name: Motion Lights
  lower_devicename: motion_front_lights
  upper_devicename: Motion_Front_Lights

esphome:
  name: $devicename

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: 

ota:
  password: 

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Optional manual IP
  manual_ip:
    static_ip: 
    gateway: 
    subnet: 255.255.255.0

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "$friendly_name Fallback Hotspot"
    password:

captive_portal:

time:
  - platform: homeassistant
    id: homeassistant_time
#time:
#  - platform: sntp
#    id: sntp_time

sun:
  latitude: !secret latitude_data
  longitude: !secret longitude_data
  id: sun_sun

binary_sensor:
  - platform: gpio
    pin:
      number: D1
      inverted: true
      mode:
        input: true
        pullup: true
    name: "Motion"
    device_class: Motion
    id: motion1
    internal: true
    on_press:
      then: 
      - if:
          condition:
            - sun.is_below_horizon
          then:
            - binary_sensor.template.publish:
                id: motion2
                state: ON
            - logger.log: "Below horizon go for it"
          else:
            - logger.log: "Sun is above horizon - no trigger"
    on_release:
      - binary_sensor.template.publish:
          id: motion2
          state: OFF
      - logger.log: "Light goes off"
  - platform: template
    name: "Motion"
    id: motion2
    device_class: Motion