Issue with Condition not stopping action

It doesn’t matter what the time is as the condition should prevent the toggle if the switch is off. But yes it should be in 24hr clock 23:30:00’ :slight_smile: . As for the - I agree it should not be needed as it’s not a list but sometimes it just only seems to work with the - :wink:

However I think petro has the problem as it’s probably not triggering at all atm.

1 Like

So I think what is happening is that HA is not using my condition because it is not identifying the state of the reed sensor. Well, it kind of does. If I click on my switch on my dashboard and the garage is closed it says ‘sensor state off’ and if the garage door is open it says ‘sensor state on’. If I manually trigger my automation when the garage door is open, the garage door will close and visa versa. This is telling me that HA is not actually reading the sensor state because in theory if my garage door was closed and I manually triggered my automation, nothing would happen because the sensor condition of ‘off’ would not be met.

Idk what my options are now. HA CAN see the sensor change because I can see it from the dashboard, but I can’t get the automation to work with it.

I did a bit more testing and my results are interesting. HA detects the ‘on’ state of the sensor just fine. If I set my state condition to ‘on’ it behaves accordingly, but obviously backwards. What’s happening is that HA is ignoring my condition state of ‘off’ and treating it as ‘on’. Essentially, if my garage door is open, it triggers to shut it and if my garage door is closed when the trigger fires, the garage door opens instead of the condition stopping the trigger.

I think I finally nailed down what’s going on. HA can’t determine what the state actually is, it’s all confused. If you look at the screenshot, it thinks the off state is on. I THINK I have to fix this using templates, but I don’t fully understand how all of that works. Any suggestions to get HA to register the states correctly? I wouldn’t mind having an ‘open’ and ‘closed’ state instead since this is a garage door.

image

Could you show a screenshot like the one above, but then with the states when the door is closed? Then we can see the exact difference between the two states, should make it easier to make a template for you.

Edit: I’m also curious as to why your sensor is defined as a ‘Momentary switch’. My first thought was that this is actually a pulse type switch which in only ‘on’ for a very short period.

If you manually trigger the condition and trigger don’t get processed just the action part, unless the condition is in the action.

Is the sensor_state an attribute? If it is you probably need a template to bring it out such that the condition will work.

Keith,
I think I’ve been going off the state of the switch this whole time and not the state of the sensor. Yes, the sensor_state is an attribute and it looks like I’ll need to make a template to make it work in my automation. I have not made templates before, what is required to do that? Do I make a new yaml file?

- alias: Shut Garage Door after 11PM
  trigger:
    - platform: time
      at: '08:49:00'
  condition:
    condition: template
    value_template: "{{ is_state_attr("switch.garage_door","sensor_state","on") }}"
  action:
    service: switch.toggle
    entity_id: switch.garage_door

Everyone, thanks for your help. A great community like this is how everyone moves forward.

With that said, I finally got it working with help from petro. I was ultimately missing the sensor state via a template condition. The sensor state value in my automation had to be ‘off’ instead of ‘on’ though. Setting it to off makes the automation trigger only if the sensor state is off, which means the garage door is open. I’ve pasted my final code below I also used the automation tool provided by HA as it made things easier to ensure proper formatting:

- action:
  - service: switch.toggle
  alias: test
  condition:
  - condition: template
    value_template: '{{ is_state_attr("switch.garage_door","sensor_state","on") }}'
  id: '1516814116515'
  trigger:
  - at: '23:00:00'
    platform: time

Except you forgot to define an entity_id to toggle, so now every switch in your setup will toggle when that automation runs :stuck_out_tongue_winking_eye:

mf_social, good call, I updated it:

- action:
  - data:
      entity_id: switch.garage_door
    service: switch.toggle
  alias: Close Garage after 11PM
  condition:
  - condition: template
    value_template: '{{ is_state_attr("switch.garage_door","sensor_state","off") }}'
  id: '1516814116515'
  trigger:
  - at: '11:19:30'
    platform: time
1 Like