Automation based on JSON Attribute states

Hi everybody,

I have a sensor (https://www.home-assistant.io/components/binary_sensor.egardia/) that outputs the state of all my door sensors into the following json:

https://pastebin.com/g9QS9Dpm

What i would like to achieve is an automation that sends me a notification if one of the door sensors changes it’s state from closed to open along with the name of the respective door that triggered the event. Unfortunately I have no clue so far as to how to accomplish this task.

Any help is highly appreciated!

Thanks

Those sensors aren’t automatically created separately in HA?

if not, you should probably make them into sensors using template sensors:

sensor:
  platform: template
  sensors:
    sensor1:
      value_template: >
        {% if binary_sensor.egardia.attributes[0]['status'] == 'Door Open' %}
          open
        {% else %}
          closed
        {% endif %}

Then build automations off that. Thats assuming that the json is unedited, and 0 is the first index of the attributes. I do not know how your binary_sensor passes the info back. Typically they are on/off only (hence binary).

1 Like

Hi @petro, thank you so much for taking your time, I really appreciate your support. The pastebin link contains the unedited json. Unfortunately I couldn’t get your solution to work, maybe due to the fact that it isn’t a binary sensor, see the screenshot.

None the less also when I change the code to:

{% if sensor.egardia_json.attributes[0]['status'] == 'Door Open' %}

it just returns “unknown” as the state of the newly created sensor. @jeroenterheerdt do you have an idea as to how I can achieve my planed setup?

quickest way is just creating automations for each binary sensor. Have a state trigger in there and you would be done. Other than that I do not know - you would probably want to somehow know who triggered an automation, but I do not think that can be captured.

1 Like

Try:

{% if sensor.egardia_json[0]['status'] == 'Door Open' %}

Just for testing purposes, try this inside the template editor:

{{ sensor.egardia_json[0]['status'] }}

What does it return?

1 Like

hi Petro,

I was on leave and didn’t have the possibility yet to thank you for your support. After fiddeling around a little more
with your suggestions it suddenly started working. Thank you very much! :+1:

1 Like

Hey,

how can I use the value diesel 1.249 in automation script?

sensor.fuel[prices]['fd99d09b-f1e8-5642-8407-542ecf56a305.diesel']  

try
sensor.fuel[0]['prices']['fd99d09b-f1e8-5642-8407-542ecf56a305.diesel']

hi,
thanks for your urgent reply.
I updated my code and got no errors, but the automation does´t work.

  - alias: diesel_guenstig
trigger:
  platform: template      
  value_template: "{{ states('input_number.schwellwert_diesel')| float >= states(sensor.fuel[0]['prices']['fd99d09b-f1e8-5642-8407-542ecf56a305.diesel']) |float }}"
action:  
  - service: notify.Pushbullet
    data:
      title: "Diesel ist gerade günstig"
      message: "Diesel ist gerade günstig: 'states.sensor.fuel[0]['prices']['fd99d09b-f1e8-5642-8407-542ecf56a305.diesel']' €" 

in the template editor
{states.sensor.fuel[0][‘prices’][‘fd99d09b-f1e8-5642-8407-542ecf56a305.diesel’].state}

does not work.

in your automation it should be:

  - alias: diesel_guenstig
  trigger:
    platform: template      
    value_template: "{{ states('input_number.schwellwert_diesel')| float >= states(sensor.fuel[0]['prices']['fd99d09b-f1e8-5642-8407-542ecf56a305.diesel']) |float }}"
  action:  
    - service: notify.Pushbullet
      data_template:
        title: "Diesel ist gerade günstig"
        message: "Diesel ist gerade günstig: {{ states.sensor.fuel[0]['prices']['fd99d09b-f1e8-5642-8407-542ecf56a305.diesel'] }} €" 

in the template editor it should be

{{ states.sensor.fuel[0]['prices']['fd99d09b-f1e8-5642-8407-542ecf56a305.diesel'] }}

the automation does not work and
in the template editor I get this error:

Error rendering template: UndefinedError: homeassistant.core.State object has no element 0

{{ states.sensor.fuel.attributes['prices']['fd99d09b-f1e8-5642-8407-542ecf56a305.diesel'] }}

ne error and no display in the editor, I try the automation

edit: automation did not work

what about

{{ states.sensor.fuel.attributes.prices }}

If this returns a string (That appears to be json), then you’ll need to parse the string to retrieve your information.

it returns:
{‘fd99d09b-f1e8-5642-8407-542ecf56a305’: {‘status’: ‘open’, ‘e5’: 1.419, ‘e10’: 1.399, ‘diesel’: 1.209}, ‘6875d242-e891-45a0-bcbf-e8aa8e2c6ace’: {‘status’: ‘open’, ‘e5’: 1.459, ‘e10’: 1.439, ‘diesel’: 1.259}, ‘e1a15081-2593-9107-e040-0b0a3dfe563c’: {‘status’: ‘open’, ‘e5’: 1.419, ‘e10’: 1.399, ‘diesel’: 1.209}, ‘51d4b655-a095-1aa0-e100-80009459e03a’: {‘status’: ‘open’, ‘e5’: 1.419, ‘e10’: 1.399, ‘diesel’: 1.209}, ‘005056ba-7cb6-1ed2-bceb-8d2e9731cd33’: {‘status’: ‘open’, ‘e5’: 1.469, ‘e10’: 1.449, ‘diesel’: 1.269}}

I have the solution:

{{ states.sensor.fuel.attributes.prices[“fd99d09b-f1e8-5642-8407-542ecf56a305”].diesel }}

I also have an Egardia alarm system, just as the topic starter, but mine is a bit older.

The “json” I get back from http://192.168.x.y/action/sensorListGet is

/*-secure-
{	senrows : [
{no : "1", type : "Remote Controller", zone : "1", name : "Afstandsbe", attr : "Personal Att", cond : "", battery : "", tamp : "", bypass : "No"},
{no : "2", type : "Remote Keypad", zone : "2", name : "Voordeur", attr : "",cond : "", battery : "", tamp : "", bypass : "No"},
{no : "3", type : "Door Contact", zone : "3", name : "Voordeur", attr : "Entry Zone", cond : "Open", battery : "", tamp : "", bypass : "No"},
{no : "4", type : "IR Sensor", zone : "4", name : "Woonkamer", attr : "Entry Zone", cond : "", battery : "", tamp : "", bypass : "No"},
{no : "5", type : "IR Sensor", zone : "5", name : "Keuken", attr : "Entry Zone", cond : "", battery : "", tamp : "", bypass : "No"},
{no : "6", type : "Door Contact", zone : "6", name : "Achterdeur", attr : "Entry Zone", cond : "", battery : "", tamp : "", bypass : "No"}]
}
*/

I believe the "/*-secure-" at the start and "*/" at the end is causing some issues.
I tried a few things in the template editor, but can’t get it to work.

How can I add the value of “cond” for both door contacts as sensors in HassIO?

That’s not json. The key’s are not encapsulated in quotes. Even if you were to remove the secure portion, it would still not work.

That’s why I had json between quotes, but was hoping it was somehow possible to manipulate the response and then convert what I need to sensors.