I was a little dissatisfied with the logbooks particularly when I went to find out recent (couple hours) events. I decided to create a event sensors for the front end that will track the state changes of specified entities. By automation and sensor templating, the last event is in sensor Event0 and second last in Event1 and so on. When the state changes off, it will shut is off event sensor and change the entity picture to white. (bulb means on) The event state start time and end time are added.
And since the forum topic is called “Share your Projects” can you please share your project posting the code?
Will do , I am looking for a method to pass variable from automation to a mqtt sensor topic. If I can find how to so this, it would make this project very simple to install and i’ll post it. As it stands, it would be very difficult for someone to follow what I did. Do you want this project? If so, I can help you implement it and walk you through my code.
Thank you! Did you try to use a template to pass the variable?
Did that work?
It did not, as a result I have to write each shut off action for each event sensor. That is 20 actions in the automation and 20 scripts to receive it. If I can pass the variable, it would be 1 action and 1 script for shut off.
- group.sump_pump_alert
- group.upstair_bedroom_windows_alert
- group.theater
- group.living_room
to: 'off'
action:
- service: group.set_visibility
data_template:
entity_id: '{{ trigger.entity_id }}'
visible: False
- service: homeassistant.turn_on
entity_id: script.offevents0
data_template:
variables:
newstate: '{{ trigger.entity_id }}'
senstate: 'group.{{ states.sensor.e0.state | lower | replace( "-","") | replace( "q","") | replace( "2","" ) | replace( "3","" ) | replace( "4","") | replace( "5","") | replace( "6","" ) | replace( "7","" ) | replace( "8","" ) | replace( "9","" ) | replace( "1","") | replace( "0","") | replace( ":","") | replace( " ","_") }}_alert'
num: 0
- service: homeassistant.turn_on
entity_id: script.offevents1
data_template:
variables:
newstate: '{{ trigger.entity_id }}'
senstate: 'group.{{ states.sensor.e1.state | lower | replace( "-","") | replace( "q","") | replace( "2","" ) | replace( "3","" ) | replace( "4","") | replace( "5","") | replace( "6","" ) | replace( "7","" ) | replace( "8","" ) | replace( "9","" ) | replace( "1","") | replace( "0","") | replace( ":","") | replace( " ","_") }}_alert'
num: 0
- service: homeassistant.turn_on
entity_id: script.offevents2
data_template:
variables:
newstate: '{{ trigger.entity_id }}'
senstate: 'group.{{ states.sensor.e2.state | lower | replace( "-","") | replace( "q","") | replace( "2","" ) | replace( "3","" ) | replace( "4","") | replace( "5","") | replace( "6","" ) | replace( "7","" ) | replace( "8","" ) | replace( "9","" ) | replace( "1","") | replace( "0","") | replace( ":","") | replace( " ","_") }}_alert'
num: 0
Any development on the issue? Can you post the code so i can test?
I have attached the code for three event sensor. I have 20 which requires just repeating the scripts and automations. I use mqtt publish to push states to event sensors (mqtt sensors). The input text is used because it the event sensor state can only be templated to that. It is used just to hold a value for comparison. The sensor.e0 hold the event name and sensor.s0 holds the state of that event. To reduce the number of events, I eliminated an event if it was the last event to occur. Please let me know if I can explain any part.
input_text:
events:
name: events
initial: ""
max: 500
sensor:
- platform: mqtt
name: e0
state_topic: "mac/0/state"
- platform: mqtt
name: e1
state_topic: "mac/1/state"
- platform: mqtt
name: e2
state_topic: "mac/2/state"
- platform: mqtt
name: e3
state_topic: "mac/3/state"
- platform: mqtt
name: s0
state_topic: "mac/0/stateonoff"
- platform: mqtt
name: s1
state_topic: "mac/1/stateonoff"
- platform: mqtt
name: s2
state_topic: "mac/2/stateonoff"
- platform: mqtt
name: s3
state_topic: "mac/3/stateonoff"
- platform: template
sensors:
event0:
value_template: "{{ states('sensor.e0')}}"
entity_picture_template: >-
{%- if is_state('sensor.s0', 'on') -%}
/local/light.gif
{%- else -%}
/local/white.gif
{%- endif -%}
- platform: template
sensors:
event1:
value_template: "{{ states('sensor.e1')}}"
entity_picture_template: >-
{%- if is_state('sensor.s1', 'on') -%}
/local/light.gif
{%- else -%}
/local/white.gif
{%- endif -%}
- platform: template
sensors:
event2:
value_template: "{{ states('sensor.e2')}}"
entity_picture_template: >-
{%- if is_state('sensor.s2', 'on') -%}
/local/light.gif
{%- else -%}
/local/white.gif
{%- endif -%}
- platform: template
sensors:
event3:
value_template: "{{ states('sensor.e3')}}"
entity_picture_template: >-
{%- if is_state('sensor.s3', 'on') -%}
/local/light.gif
{%- else -%}
/local/white.gif
{%- endif -%}
scripts:
events:
sequence:
- condition: template
value_template: '{{ states.input_text.events.state | replace ("z", "") | lower | replace ("-", "") !=states.sensor.e0.state | replace ("z", "") | replace ("-", "") | lower | replace( "-","") | replace( "q","") | replace( "2","" ) | replace( "3","" ) | replace( "4","") | replace( "5","") | replace( "6","" ) | replace( "7","" ) | replace( "8","" ) | replace( "9","" ) | replace( "1","") | replace( "0","") | replace( ":","") }}'
- service: mqtt.publish
data:
topic: "mac/3/stateonoff"
payload_template: |
{{ states.sensor.s2.state}}
retain: True
- service: mqtt.publish
data:
topic: "mac/2/stateonoff"
payload_template: |
{{ states.sensor.s1.state}}
retain: True
- service: mqtt.publish
data:
topic: "mac/1/stateonoff"
payload_template: |
{{ states.sensor.s0.state}}
retain: True
##########################################################################################
##########################################################################################
##########################################################################################
- service: mqtt.publish
data:
topic: "mac/3/state"
payload_template: |
{{ states.sensor.e2.state}}
retain: True
- service: mqtt.publish
data:
topic: "mac/2/state"
payload_template: |
{{ states.sensor.e1.state}}
retain: True
- service: mqtt.publish
data:
topic: "mac/1/state"
payload_template: |
{{ states.sensor.e0.state}}
retain: True
##########################################################################################
##########################################################################################
offevents0:
sequence:
- condition: template
value_template: '{{ [ newstate ] == [ senstate ] }}'
- service: mqtt.publish
data:
topic: "mac/0/state"
payload_template: |
{{ states.sensor.e0.state}}{{ states.sensor.time.state}}
retain: True
- service: mqtt.publish
data:
topic: "mac/0/stateonoff"
payload_template: 'off'
retain: True
offevents1:
sequence:
- condition: template
value_template: '{{ [ newstate ] == [ senstate ] }}'
- service: mqtt.publish
data:
topic: "mac/1/state"
payload_template: |
{{ states.sensor.e1.state}}{{ states.sensor.time.state}}d
retain: True
- service: mqtt.publish
data:
topic: "mac/1/stateonoff"
payload_template: 'off'
retain: True
offevents2:
sequence:
- condition: template
value_template: '{{ [ newstate ] == [ senstate ] }}'
- service: mqtt.publish
data:
topic: "mac/2/state"
payload_template: |
{{ states.sensor.e2.state}}{{ states.sensor.time.state}}d
retain: True
- service: mqtt.publish
data:
topic: "mac/2/stateonoff"
payload_template: 'off'
retain: True
offevents3:
sequence:
- condition: template
value_template: '{{ [ newstate ] == [ senstate ] }}'
- service: mqtt.publish
data:
topic: "mac/3/state"
payload_template: |
{{ states.sensor.e3.state}}{{ states.sensor.time.state}}d
retain: True
- service: mqtt.publish
data:
topic: "mac/3/stateonoff"
payload_template: 'off'
retain: True
##########################################################################################
##########################################################################################
automation event_on:
alias: event sensor on
trigger:
- platform: state
entity_id:
#####Change these to the sensors you want to monitor
- group.upstair_bedroom_windows_alert
- group.theater
- group.living_room
to: 'on'
action:
- service: input_text.set_value
data_template:
entity_id: input_text.events
value: '---{{ trigger.to_state.attributes.friendly_name | replace(" Alert","") }}zzzzzzzzzzzzzzzzzzzzzzzzzzz'
- service: homeassistant.turn_on
entity_id: script.events
- service: mqtt.publish
data:
topic: "mac/0/state"
payload_template: '{{ states.sensor.time.state}}{{ states.input_text.events.state | truncate(25, True, "") | replace ("z","-") }}'
- service: mqtt.publish
data:
topic: "mac/0/stateonoff"
payload_template: 'on'
automation event_off:
alias: event sensor off
trigger:
- platform: state
entity_id:
#####Change these to the sensors you want to monitor
- group.upstair_bedroom_windows_alert
- group.theater
- group.living_room
to: 'off'
action:
- service: homeassistant.turn_on
entity_id: script.offevents0
data_template:
variables:
newstate: '{{ trigger.entity_id }}'
senstate: 'group.{{ states.sensor.e0.state | lower | replace( "-","") | replace( "q","") | replace( "2","" ) | replace( "3","" ) | replace( "4","") | replace( "5","") | replace( "6","" ) | replace( "7","" ) | replace( "8","" ) | replace( "9","" ) | replace( "1","") | replace( "0","") | replace( ":","") | replace( " ","_") }}_alert'
num: 0
- service: homeassistant.turn_on
entity_id: script.offevents1
data_template:
variables:
newstate: '{{ trigger.entity_id }}'
senstate: 'group.{{ states.sensor.e1.state | lower | replace( "-","") | replace( "q","") | replace( "2","" ) | replace( "3","" ) | replace( "4","") | replace( "5","") | replace( "6","" ) | replace( "7","" ) | replace( "8","" ) | replace( "9","" ) | replace( "1","") | replace( "0","") | replace( ":","") | replace( " ","_") }}_alert'
num: 0
- service: homeassistant.turn_on
entity_id: script.offevents2
data_template:
variables:
newstate: '{{ trigger.entity_id }}'
senstate: 'group.{{ states.sensor.e2.state | lower | replace( "-","") | replace( "q","") | replace( "2","" ) | replace( "3","" ) | replace( "4","") | replace( "5","") | replace( "6","" ) | replace( "7","" ) | replace( "8","" ) | replace( "9","" ) | replace( "1","") | replace( "0","") | replace( ":","") | replace( " ","_") }}_alert'
num: 0
- service: homeassistant.turn_on
entity_id: script.offevents3
data_template:
variables:
newstate: '{{ trigger.entity_id }}'
senstate: 'group.{{ states.sensor.e3.state | lower | replace( "-","") | replace( "q","") | replace( "2","" ) | replace( "3","" ) | replace( "4","") | replace( "5","") | replace( "6","" ) | replace( "7","" ) | replace( "8","" ) | replace( "9","" ) | replace( "1","") | replace( "0","") | replace( ":","") | replace( " ","_") }}_alert'
num: 0
But how to integrate with my system? Should i change the name of e0, e1, e2, e3, s0, s1, s2 and s3 to the name of my sensors? There is something i’m not getting…
The screenshot from the first post is a group called events like this
Events:
entities:
- sensor.event0
- sensor.event1
- sensor.event2
- sensor.event3
- sensor.event4
the other sensors like e1 or s1 are not used for display, just to hold values.
The only thing you need to do is add a group like I mentioned above and change the groups in the automation to entities you want to monitor. I actual use it to monitor groups not entities but it will work just the same with entities. You will need to adjust the senstate: so it matches the entities you put in. I used groups so it starts with groups.