So if you just rewrote your if else if statements to output comma separated values (without keywords) instead of json objects. I.e “message, severity”, You could do this:
{% set m_test_json = states.sensor.front_door_last_state.state.split(',') %}
Result with the Sensor state
Message: {{ m_test_json[0] }}
Severity: {{ m_test_json[1] }}
Also, if you were feeling up to it, you could attempt to streamline your if statements so that there is less overhead if you have to add codes or names into your sensor. This is what I would use because you’d just need to add a code and a name, or a code and a statement.
sensor:
- platform: template
sensors:
front_door_last_state:
friendly_name: Front Door Last Code
entity_id:
- sensor.frontlock_alarm_type
- sensor.frontlock_alarm_level
value_template: >-
{% set danger = [ 9, 161 ] %}
{% set who = {
1: 'laurent',
2: 'Valerie',
3: 'Juliette',
4: 'Lucas',
5: 'Chloé',
6: 'Any and Hubert',
7: 'Myriam and Hervé',
8: 'Agnès',
} %}
{% set statements = {
9: 'Lock Jammed',
18: 'Locked via Outside Button',
21: 'Locked via Thumbturn',
22: 'Unlocked via Thumbturn',
24: 'Wireless Lock',
25: 'Wireless Unlock',
27: 'Auto Locked',
161: 'Tamper Alarm',
} %}
{% set alarm_type = states('sensor.frontlock_alarm_type') | int %}
{% set alarm_level = states('sensor.frontlock_alarm_level') | int %}
{% if alarm_type == 19 %}
{% if alarm_level in who %}
Unlocked by {{ who[alarm_level] }}, good
{% else %}
Unlocked by an Untracked User Code, good
{% else %}
{% if alarm_type in statements %}
{{ statements[alarm_type] }}, {{ 'danger' if alarm_type in danger else 'good' }}
{% else %}
Unknown due to HA restart, warning
{% endif %}
{% endif %}
- platform: template
sensors:
front_door_last_state:
friendly_name: Front Door Last Code
entity_id:
- sensor.frontlock_alarm_type
- sensor.frontlock_alarm_level
value_template: >-
{% set danger = [ 9, 161 ] %}
{% set who = {
1: 'laurent',
2: 'Valerie',
3: 'Juliette',
4: 'Lucas',
5: 'Chloé',
6: 'Any and Hubert',
7: 'Myriam and Hervé',
8: 'Agnès',
} %}
{% set statements = {
9: 'Lock Jammed',
18: 'Locked via Outside Button',
21: 'Locked via Thumbturn',
22: 'Unlocked via Thumbturn',
24: 'Wireless Lock',
25: 'Wireless Unlock',
27: 'Auto Locked',
161: 'Tamper Alarm',
} %}
{% set alarm_type = states('sensor.frontlock_alarm_type') | int %}
{% set alarm_level = states('sensor.frontlock_alarm_level') | int %}
{% if alarm_type == 19 %}
Good Idea Petro. I like your solution. Much more easy to read than the one I suggested !
I’m going to give your solution a try tomorrow and let you know !
I’m a home assistant beginner but I love it.
The sensor Works perfect (If someone needs to use this code, there is a {% endif %} missing).
now, i’m wondering how may I “parse” the sensor state in an automation.
I’d like to use the part before the comma as a message and the part after the comma for the color of the message I will send as a notification
Ok, replying to my own question. The split function does the trick
Just use {{ states.sensor.xxx.state.split(",")[0]}} or [1] for the part of the message before or after the comma !
Thank you a lot. I learned many things thanx to you today
I am back to really wanting this functionality to work in templates. How hard is it to add a filter to the templating language? I’ll even attempt it myself.
Rooting for your PR to quickly run the gamut and get included in the next release. Like the recent inclusion of the expand filter, the from_json filter will open up new ways of solving old templating problems.
Nice, I see you’re a software dev by trade. Let me tell you, python is easy. I work in python and c# for work and have no issues switching between the two. I prefer strongly typed languages, and you can do that if you want in python. My personal biggest gripe about python is that you cannot have more than 1 constructor for a method. But if you use **kwargs you can pretty much get the same functionality.
My job is all Microsoft all the time. I’m very fluent in C# and PowerShell, but writing a tiny bit of python makes me realize how much I’ve become dependent on Intellisense in Visual Studio.