Turning off a light at 7pm every night (using a lambda to check the time)

just need a few pointers on how to format a lambda call to check when its gone 7pm to turn off a led sign ive made.

things ive tried so far

 text_sensor:
      - platform: template
        id: onoff
        lambda:
          if (id(sntp_time).now().hour > 19 {
            light.turn_off: sign_light;
            return id(print_status).state = "Off";
          } else {
            id(Daytime).execute();
            return id(print_status).state = "on";
          }
        update_interval: 1s 

-text sensor - above (wouldn’t compile)

also

time:
  - platform: sntp
    # ...
    on_time:
      - seconds: 0
        minutes: 0
        hours: 7
        days_of_week: MON-SUN
        then:
          - script.execute: 
              id: Daytime
              brightness: 50
      - seconds: 0
        minutes: 0
        hours: 19
        days_of_week: MON-SUN
        then:
          - light.turn_off: sign_light

      # Cron syntax, trigger every 5 minutes
      - cron: '0 /60 * * * *'
        then:        
          - if:
              condition:
                - sun.is_above_horizon:
              then:
                - light.turn_on:
                   id: sign_light
                   effect: Fireworks
                - delay: 10s
                - script.execute: Daytime

-on_time turned off but the system turned on (ran the script) again a bit later

Many Thanks

D

Show the configuration you used for this. Likely you just need to adjust your cron settings.

done and many thanks

to be honest I don’t fully understand the lambda gig or the time formatting but im getting there

Ok, that looks like you are running the Daytime script at 7am and turning the sign off at 7pm, every day of the week.

Also you are running the fireworks effect then the Daytime script every 5 minutes if the sun is above the horizon.

So if sunset is after 7pm (or sunrise is before 7am), you will run the daytime script every 5 minutes turning the display back on even if it is outside your 7am to 7pm schedule.

There is no time condition so yeah, you have to use a lambda condition. This will run your effect every 5 minutes (crontab fixed as well) between 7am and 7pm

      - cron: '*/5 * * * *'
        then:        
          - if:
              condition:
                - lambda: 'return id(sntp_time).now().hour > 6;'
                - lambda: 'return id(sntp_time).now().hour < 19;'
              then:
                - light.turn_on:
                   id: sign_light
                   effect: Fireworks
                - delay: 10s
                - script.execute: Daytime

Edit: you could also write it like this I think:

      - cron: '*/5 * * * *'
        then:        
          - if:
              condition:
                - lambda: 'return ( 6 < id(sntp_time).now().hour < 19 );'
              then:
                - light.turn_on:
                   id: sign_light
                   effect: Fireworks
                - delay: 10s
                - script.execute: Daytime
2 Likes

much love buddy …