I fixed an automation where I forgot a template and did test it, and it worked, but when I rebooted it sent HAOS into recovery mode with a syntax error. When I say it worked, it seemed to, though I did not test exhaustively with a long smoke detector alarm, it just did the first loop and quit.
Here’s the full template except the failing line is commented out and replaced with a specific entity (that doesn’t fail syntax). Look near the end in the condition: state
It’s like the syntax check is the issue?
Or is there an actual syntax error?
I tried a value template but I get something similar, since neither trigger from nor to state is what I want – I want then now-current state of the entity that resulted in the trigger.
alias: Handle Smoke Detectors
description: One automation, all smoke detectors
id: handle_smoke_detectors
triggers:
- platform: state
id: fire_id
entity_id:
- binary_sensor.lab_smoke
- binary_sensor.small_garage_smoke
- binary_sensor.pantry_garage_smoke
- binary_sensor.hall_smoke
- binary_sensor.main_garage_smoke
- binary_sensor.laundry_smoke
- binary_sensor.livingroom_smoke
not_to:
- unknown
- unavailable
- "off"
- platform: state
id: testing_id
entity_id:
- binary_sensor.lab_smoke_test_in_progress
- binary_sensor.small_garage_smoke_test_in_progress
- binary_sensor.pantry_garage_smoke_test_in_progress
- binary_sensor.hall_smoke_test_in_progress
- binary_sensor.main_garage_smoke_test_in_progress
- binary_sensor.laundry_smoke_test_in_progress
- binary_sensor.livingroom_smoke_test_in_progress
not_to:
- unknown
- unavailable
- "off"
actions:
- choose:
- conditions:
- condition: trigger
id: testing_id
sequence:
- action: nodered.trigger
data:
entity_id: switch.announce_and_email_config_node
message:
speaktext: >
{% set area = trigger.entity_id.replace('binary_sensor.','').replace('_smoke_test_in_progress','') %}
Smoke detector test ongoing in {{ area }}
emailsubject: "Smoke detector test ongoing in {{ area }}"
emailbody: "Smoke detector test ongoing in {{ area }}"
- conditions:
- condition: trigger
id: fire_id
sequence:
- repeat:
until:
- condition: state
# entity_id: {{ trigger.entity_id }}
entity_id: smoke.lab_smoke
state: "off"
sequence:
- action: nodered.trigger
data:
entity_id: switch.announce_and_email_config_node
message:
speaktext: "Fire. Fire. Fire in {{ trigger.entity_id.replace('binary_sensor.','').replace('_smoke','') }} "
emailsubject: "Fire. Fire. Fire in {{ trigger.entity_id.replace('binary_sensor.','').replace('_smoke','') }} "
emailbody: "Fire. Fire. Fire in {{ trigger.entity_id.replace('binary_sensor.','').replace('_smoke','') }} "
- delay:
seconds: 20
mode: restart
Crap… I knew that. I was concentrate on all the blueprint rules (I was coming from them) and trying to make sure i could do this.
Well, quotes do make the syntax work on the “check” but the automation fails to set up complaining.
Error:Entity {{ trigger.entity_id }} is neither a valid entity ID nor a valid UUID for dictionary value @ data['actions'][0]['choose'][1]['sequence'][0]['repeat']['until'][0]['entity_id']. Got '{{ trigger.entity_id }}' required key not provided @ data['actions'][0]['choose'][1]['sequence'][0]['repeat']['until'][0]['state']. Got None.
So is there any way I can actually do this – to have one action that processes these steps based on which entity alarmed?
Interestingly the templates in the part that follows work fine. So it doesn’t SEEM to be a template-not-valid-here thing with regard to the action overall.
That works. Thank you. Jinja has too many things that are almost the same, I just can’t keep up with which works. I was trying trigger.entity_id.state which I had seen in places, but I think those places were also in error.
Two other things… First, you may want to consider whether restart is the best mode for this. And second, you may want to create an Input boolean helper to add as OR in the repeat condition. That will give you a way to override the alert for non-emergency smoke events.
So… I’m trying to work out in my mind how that actually goes. I did restart under the idea that if smoke A went off, and is going off in the loop, and 30 seconds later smoke B went off, I THINK i want it to just start over with “B” and start announcing fire in that location.
But your “how to stop it” is a good point. I was thinking I would, well, just stop it. But I might not actually be able to. Though I can always stop node red where the actual notifications are coming out.
I’m using node red as I have all sorts of timing and semaphores in place to handle how overlapping voice alerts work with google, it works, and I wasn’t quite ready to reproduce in script.
But need to think about it… a voice “Activate smoke silence” might be a good thing.
Indeed. It’s kind of like HA’s various syntax checks, the same line passes some, fails others, and I swear in think the syntactically wrong version ran correctly though it’s not worth chasing down to find out.
I will say that having an automation with a syntax error through HA into recovery mode on restart was a bit of a shock.
Well, you did something a yaml parser can’t parse. What you did would fail outside HA too.
You provided the yaml with:
entity_id: {{ trigger.entity_id }}
With in regards to yaml, you told it that entity_id was a dictionary. Yaml dictionaries require a key value pair. You only provided the key with invalid characters.
e.g. long hand yaml, you did this:
entity_id:
'{ trigger.entity_id }'
but you left out the key and you used special characters in the key. ( {, ., and })
This is why learning yaml is important.
Just understand that this:
something: {a:b}
is the same as:
something:
a: b
And that’s what you got bit by. To equate this to what you provided. If you replace something with entity_id…
entity_id: {a:b}
Now replace a with what you provided for the key a…
entity_id: {{ trigger.entity_id }:b}
Notice how your original yaml was missing the : and the value for b? That’s where the yaml parser failed to parse the yaml you provided and forced HA into safe mode.
This is something we cannot catch, which is why you went into safe mode.
Thank you, that’s helpful toward understanding. Though maybe I do need to go back and try running it (with wrong syntax). My memory is old and failing, but I swear I tested it.
My complaint about safe mode is not that the syntax wasn’t wrong, but that going into safe mode seems a bit of an over-reaction to a syntax error in a single automation.
Actually now that I say that – I think that a restart runs the syntax check and won’t restart if it’s wrong. Now I’m wondering how I reached that point.
I think if I get some time later I need to checkpoint my VM and try recreating the original situation.
It’s not that I doubt your answers – it’s clearly the right approach. I just don’t know how I got where I got when this happened. Kind of like waking up somewhere you don’t remember going to sleep.
The problem is, the yaml parser cannot parse the rest of the file. The whole file is bad at that point, so no automations would come through. If that’s in your configuration.yaml file, then your whole configuration is broken. All we can do is assume this is the case and go into safe mode.
Well, it’s an include, but I get your point. Though doesn’t the current (sorry, don’t know the name of it in the yaml terminology but the current automation item) end based on indentation?
The parser uses spacing to determine objects, however you chose not to use spaces and to use the raw objects.
E.g.
normal:
yaml:
spacing: valid
normal:{yaml:{spacing:valid}}
Both are valid yaml structures and both have an identical result when parsed, your screw up was the version without spacing. I.e. You essentially screwed up the spacing.
So to reiterate you essentially provided this to yaml:
But why couldn’t it find the end, when the next automation started (actually in this case the end of the file), and just invalidate everything up to that point.
It’s not really that important - in this case recovery mode worked fine. Once long ago I had a syntax error near the top of configuration.yaml, in the trusted network section. It sent HA into… I think it was safe mode not recovery mode… and lost almost everything. Complete mess, had to reconstruct lots of integrations by hand and by parsing out old files by eye; this pre-dated the automatic backups. Since then I keep regular snapshots every time I do something significant. But i’ve been paranoid ever since that of HA sending itself into recovery/safe mode.
Thank you again for helping me understand. If I find something interesting in trying to recreate my original state will post, otherwise will try to understand yaml syntax better from what you wrote. I’ve been programming for literally 50+ years, and yaml combined with templates is just really tough for me to understand.
The parser while searching for a dictionary is looking for the value which you did not supply and it caused an exception. Keep in mind, we did not create the parser, we are using a standard yaml parsing lib provided by python.
I put what I THOUGHT I had when it failedl; it won’t restart because the syntax check catches it.
Here’s the rub… whatever I had DID restart, because it was on the restart that it found it and went into recovery mode. Wasn’t a power failure or anything like that.
So I’m baffled. I do understand the syntax was wrong, mea culpa.
But I don’t know how I managed to get into recovery mode, at least not with what I THOUGHT was there.
So… woke up somewhere different than I remember going to sleep. That’s bad.
Again, thank you @petro for helping me understand better.