I’ve read through this
https://community.home-assistant.io/t/automation-with-for-each-and-if/422732
and specifically the response which says to use
if: "{{ is_state(repeat.item.window, 'off') }}"
and I still cannot get this to work
What I’m trying to do is to count the number of switches which are in the state matching a variable , I twant to use for each/if but I gave up and manual duplicaed the code block 9 times - - I change “switch.orchard2_on_board_d2” in each duplicated block This is the repeated code block
if:
- condition: state
entity_id: switch.orchard2_on_board_d2
state:
- "on"
then:
- if:
- condition: numeric_state
entity_id: counter.d2_expectedstate
above: 0
then:
- action: counter.increment
target:
entity_id: counter.switch_counter
else:
- if:
- condition: numeric_state
entity_id: counter.d2_expectedstate
below: 1
then:
- action: counter.increment
target:
entity_id: counter.switch_counter
This is the foreach attempt
repeat:
for_each:
- switch.orchard2_on_board_d2
- switch.orchard3_on_board_d2
- switch.orchard4_on_board_d2
- switch.orchard5_on_board_d2
- switch.orchard6_on_board_d2
- switch.temp_2e8994_on_board_d2
- switch.temp_60cd54_on_board_d2
- switch.meterbox_on_board_d2
- switch.pumpshedpump_on_board_d2
sequence:
- if:
- condition: template
value_template: >-
{{ states.repeat.state ==
states.input_text.d2_expectedstatestr.state }}
then:
- action: counter.increment
metadata: {}
target:
entity_id: counter.switch_counter
data: {}
- action: notify.mobile_app_pixel_7a
metadata: {}
data:
title: |-
{{ states.repeat.state }}
{{ states.switch.orchard2_on_board_d2.state }}
{{ states.input_text.d2_expectedstatestr.state }}
message: "Hello this is the state "
What I see in the messages to the mobile app is
"None on on "Hello this is the state "
so its just “repeat” is not correct - but I dont know what I’m meant to be doing

