Turning light on if raining on weekdays only

Hi Guys, i have the below code running well. It turns on a light if rain is expected in 3 days time.

The problem is its for work and i dont need it to tell me on sunday if its raining i only need it to dectect the next 3 work days.

alias: Wrap Ply BOM
description: When rainy, turn Wrap Ply light on. This also includes a webhook overide
trigger:
  - platform: time_pattern
    hours: /2
  - platform: time_pattern
    minutes: /1
    enabled: false
condition:
  - condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
    after: "05:00:00"
    before: "18:00:00"
  - condition: or
    conditions:
      - condition: numeric_state
        entity_id: sensor.cardiff_rain_chance_0
        above: 60
      - condition: numeric_state
        entity_id: sensor.cardiff_rain_chance_1
        above: 60
      - condition: numeric_state
        entity_id: sensor.cardiff_rain_chance_2
        above: 60
  - condition: not
    conditions:
      - condition: state
        entity_id: light.wled_segment_1
        state: "on"
action:
  - metadata: {}
    data: {}
    target:
      entity_id: light.wled_segment_1
    action: light.toggle
mode: single
1 Like

So how does it not work?
You have mon-fri as a condition, do you have a trace of it running on the weekend?

It actually works well but it just doesnt run on Sat, Sun, i need it to not even detect rain on Sat, Sun.
So on Friday if the rain chance for chance 1 or 2 is greater than 60 it fires. This isnt intended as we will be wrapping ply on a Friday for a Monday that no rain is expected.

I think I understand what you are saying.

So on Friday it should turn on only if it’s raining on Friday or Monday.
On a Thursday it should look at Thursday and Friday only, and so on…

In that case perhaps this works. I can’t test it myself but you could probably test the code in developer tools.

{% set list = ["sensor.cardiff_rain_chance_0", "sensor.cardiff_rain_chance_1", "sensor.cardiff_rain_chance_2"] %}
{%- set ns = namespace( bool = false ) -%}
{% for ent in list -%}
 {%- set day = ent[-1] -%}
 {% if now().weekday() + day |int <= 4 %}
   {% if states(ent) |int > 60 %} 
     {% set set ns.bool = true %}
   {% endif -%}
 {% endif -%}
{%- endfor %}

{{ ns.bool }}

Hi mate,

i get this error, not sure if im doing it wrong. please also see my new automation below

Message malformed: invalid template (TemplateSyntaxError: expected token 'end of statement block', got 'ns') for dictionary value @ data['condition'][0]['value_template']

Heres how i edited the automation:

description: When rainy, turn Wrap Ply light on. This also includes a webhook overide
mode: single
trigger:
  - platform: time_pattern
    hours: /2
  - platform: time_pattern
    minutes: /1
    enabled: false
condition:
  - condition: template
    value_template: >-
      {% set list = ["sensor.cardiff_rain_chance_0",
      "sensor.cardiff_rain_chance_1", "sensor.cardiff_rain_chance_2"] %}

      {%- set ns = namespace( bool = false ) -%}

      {% for ent in list -%}
       {%- set day = ent[-1] -%}
       {% if now().weekday() + day |int <= 4 %}
         {% if states(ent) |int > 60 %} 
           {% set set ns.bool = true %}
         {% endif -%}
       {% endif -%}
      {%- endfor %}


      {{ ns.bool }}
action:
  - metadata: {}
    data: {}
    target:
      entity_id: light.wled_segment_1
    action: light.toggle
alias: New Wrap Ply

You’ve got an extra set command:

It should be:

{% set ns.bool = true %}

1 Like

Hi All, I appreciate all the help. Thanks for sharing your knowledge these solutions worked well.