Hello,
I read a lot of documentation and blogs online, and so far I find it hard to grasp the basics of templating, blueprint and so one. I guess this is the normal learing curve.
Here is what I’d like to achieve thru automation. I’m using Restfull as discribed here Restfull and would like to dynamically send the state of the trigger that fired the event (in that case disarmed, arming, or triggered) and the name of the entity as well as the entity id that triggered the alarm. How could this be done?
The payload I am expecting to build is something like:
{“triggerCondition”: “{{ ??? }}”,“entityName”: “{{ ??? }}”,“entityID”: “{{ ??? }}”}
As a side question the only way I found so far to update the rest_command that is sitting in configuration.yaml is to restart the server. Is there an alternative way?
Thank you very much in advance to help me on this matter.
So after reading various tuto’s on alternative sites, I came to the conclusion that I had to set a trigger to a template. Here is the code
trigger:
- platform: template
value_template: '{{ is_state(''alarm_control_panel.5656_ccc_zone_4_alarm'', ''disarmed'')
}}'
id: '4'
- platform: template
value_template: '{{ not(is_state(''alarm_control_panel.5656_ccc_zone_4_alarm'',
''disarmed'')) }}'
id: '4'
condition: []
action:
- service: rest_command.ajax2dz_event
data:
state: '{{states(''alarm_control_panel.5656_ccc_zone_4_alarm'')}}'
zone: '{{state_attr(''alarm_control_panel.5656_ccc_zone_4_alarm'',''last_zone'')}}'
mode: single
From documentation:
Triggers are what starts the processing of an automation rule. When any of the automation’s triggers becomes true (trigger fires ), Home Assistant will validate the [conditions], if any, and call the [action].
This is why there are two conditions in order to get a true result whatever the state change of alarm. The id parameter is not used at this time.
The rest command is defined in configuration file
rest_command:
ajax2dz_event:
url: '.... &data={"state": "{{state}}", "zone": "{{zone}}" }'
method: get
Since there are 4 zones, this code has been duplicated 3 more times. I’m wondering how this code could be factorized. Using a group that I started to experiment without sucess so far. Any idea?
In order to build the state and zone line programatically, I tried to set a variable inside the trigger section that would be used to replace the “4” by this variable. No success so far. Any ideas?
Thank you
Edit : here is what I came to. Only one piece of code. I tried to play with groups in order to make it even more compact. Unfortunately this is a dead end, since it is needed to get a event fired as soon as one element changes its states. As of now, a group will fire and event when the one element state goes to on and will stay on until all elements are back to off.
Automation file
- id: '1642926428231'
alias: Alarm integration with dz
description: ''
trigger:
- platform: state
entity_id: alarm_control_panel.5656_ccc_zone_4_alarm
- platform: state
entity_id: alarm_control_panel.5656_ccc_zone_3_alarm
- platform: state
entity_id: alarm_control_panel.5656_ccc_zone_2_alarm
- platform: state
entity_id: alarm_control_panel.5656_ccc_zone_1_alarm
condition:
- condition: template
value_template: '{{ (state_attr(trigger.entity_id,''last_zone'')) | int != 0 }}'
action:
- service: rest_command.ajax2dz_event
data:
state: '{{states(trigger.entity_id)}}'
zone: '{{state_attr(trigger.entity_id,''last_zone'')}}'
deviceClass: '{{state_attr(trigger.entity_id,''device_class'')}}'
lastMessage: '{{state_attr(trigger.entity_id,''last_message'')}}'
mode: single
Configuration file
rest_command:
ajax2dz_event:
url: !secret dzCustomEvent
method: get
Secret file
# Use this file to store secrets like usernames and passwords.
# Learn more at https://www.home-assistant.io/docs/configuration/secrets/
some_password: welcome
dzCustomEvent:
'http://xxxxxx/json.htm?username=xxxxxx==&password=xxxxxx=
&type=command¶m=customevent&event=ha&data={"state": "{{state}}", "zone": "{{zone}}", "deviceClass": "{{deviceClass}}", "lastMessage": "{{lastMessage}}"}'