Why doesnt this automation trigger automatically

thank you, trying this next:

message: >
 {{ as_timestamp(trigger.event.data.new_state.last_updated)|timestamp_local }}: {{ trigger.event.data.new_state.name }}

resulting in:

2018-07-21T09:54:53.938134+00:00 2018-07-21 11:54:44: Call Service Event (Script)
2018-07-21T09:55:08.208271+00:00 2018-07-21 11:55:00: Call Service Event (Script)
2018-07-21T09:55:09.870117+00:00 2018-07-21 11:55:03: Call Service Event (Script)
2018-07-21T09:55:10.188106+00:00 2018-07-21 11:55:03: Sense Active change
2018-07-21T09:55:12.629398+00:00 2018-07-21 11:55:08: Sense Active change
2018-07-21T09:55:12.656599+00:00 2018-07-21 11:55:09: Sense Active change
2018-07-21T09:55:13.569690+00:00 2018-07-21 11:55:10: Sense Active change
2018-07-21T09:55:15.674322+00:00 2018-07-21 11:55:13: Call Service Event (Script)
2018-07-21T09:55:22.950335+00:00 2018-07-21 11:55:22: Sense Active change
2018-07-21T09:55:23.889922+00:00 2018-07-21 11:55:23: Sense Active change
2018-07-21T09:55:25.255489+00:00 2018-07-21 11:55:24: Call Summary

id say, almost thereā€¦ now how to only show the last bit, from the local time 2018-07-21 11:55:24: Call Summary, maybe even the time part, cause the date is rather superfluous here.

If you want just the time (and now that I know youā€™re working with a datetime), you can do this:

{{ trigger.event.data.new_state.last_updated.strftime('%H:%M:%S') }}

As far as removing the first date/time on each line, I guess Iā€™m not sure of the complete context here. Are these going into your HA log? If so, I think the logging system automatically outputs that. Honestly, Iā€™d probably have to do a lot of digging to figure that out, even once I knew the context.

no these are going into the file through the notify.file https://www.home-assistant.io/components/notify.file/:

service: notify.filed_automation_triggered

creating the filed_automation_triggered.txt in the config directory, listing all triggered automations

Right on that page is the answer. :wink: Use the optional timestamp parameter and set it to False.

tis is not itā€¦

before:
2018-07-21T13:16:59.750487+00:00 2018-07-21 15:16:59: Sense Active change
after:
2018-07-21T13:20:44.140325+00:00 13:20:34: Startup HA

Aha, so the timestamp=true renders the utc timeā€¦ and I thought it was so easy using thatā€¦false it is!

Apparently the default is false. And I just verified that looking at the code. So you could just simply remove that parameter from your configuration.

1 Like

bingo!

2018-07-21T13:47:56.829825+00:00 13:47:17: Count warnings
2018-07-21T13:47:56.837879+00:00 13:47:17: Sense Active change
2018-07-21 15:51:25: Average indoor temp
2018-07-21 15:51:25: Startup HA
2018-07-21 15:51:28: Sense Switches change
2018-07-21 15:51:28: Sense Appliances change

using:

- alias: Automation ran
  trigger:
    platform: event
    event_type: state_changed
  condition:
    - condition: template
      value_template: >
        {{ trigger.event.data.entity_id.startswith('automation.') and
           trigger.event.data.entity_id != 'automation.automation_ran' and
           'old_state' in trigger.event.data and 'new_state' in trigger.event.data }}
  action:
    - condition: template
      value_template: >
        {{ trigger.event.data.new_state.attributes.last_triggered !=
           trigger.event.data.old_state.attributes.last_triggered }}
    - service: notify.filed_automation_triggered
      data_template:
        message: >
         {{ as_timestamp(trigger.event.data.new_state.last_updated)|timestamp_local }}: {{ trigger.event.data.new_state.name }}
    - service: python_script.last_automation
      data_template:
        event: "{{ trigger.event }}"

and:

notify:
- name: filed_automation_triggered
  platform: file
  filename: filed_automation_triggered.txt
  timestamp: False

thanks @pnbruckner youā€™ve made my day!

1 Like

since this has been an inspiration to find solutions-before-not-thought-possible, maybe this can be done too:

now, if sensing the state of entities in a group, and displaying the changed entity (and not the complete group) we have to list all entities separately in the trigger part.

cant we use the above:

 trigger:
        platform: event
        event_type: state_changed 

followed by a condition, in which we check if the trigger entity belongs to a group? I have a group.all_lights_only, in which some 24 lights are listed. I have these lists now too in the trigger part of my automation, which is prone to errors, if one of the lights is taken out or some are added.

Were i able to use the group condition , that would be so much easier and more robust.

 condition:
    - condition: template
      value_template: >
        {{ trigger.event.data.entity_id in group.all_lights_only}}

If the group would not be a possibility , maybe with the same condition as used here, changed into the following:?

  condition:
    - condition: template
      value_template: >
        {% set skip_list = ['entertainment_light1', 'entertainment_light2'] %}
        {{ trigger.event.data.entity_id.startswith('light.') and
           trigger.event.data.entity_id.split('.')[1] not in skip_list and
           'old_state' in trigger.event.data and 'new_state' in trigger.event.data }}

testing this now:

- alias: Call service event (Light)'
  id: 'Call service event (Light)'
  trigger:
    platform: event
    event_type: state_changed
  condition:
    - condition: template
      value_template: >
        {% set skip_list = ['group_for_wakeup', 'group_for_wakeup_2', 'entertainmentruimte_2', entertainmentruimte_1] %}
        {{ trigger.event.data.entity_id in group.all_lights_only and
           trigger.event.data.entity_id.split('.')[1] not in skip_list and
           'old_state' in trigger.event.data and 'new_state' in trigger.event.data }}  
  action:
    - condition: template
      value_template: >
        {{ trigger.event.data.new_state.attributes.last_triggered !=
           trigger.event.data.old_state.attributes.last_triggered }}
    - service: python_script.summary
      data_template:
        event: "{{ trigger.event }}"

hmm: 2018-07-22 15:41:05 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: 'group' is undefined

Replace group.all_lights_only with state_attr('group.all_lights_only','entity_id').

yes, that works!
changed and shortened to this now, working fine.

- alias: Call service event (Light)'
  id: 'Call service event (Light)'
  trigger:
    platform: event
    event_type: state_changed
  condition:
    - condition: template
      value_template: >
        {% set skip_list = ['group_for_wakeup', 'group_for_wakeup_2', 'entertainmentruimte_2', entertainmentruimte_1] %}
        {{ trigger.event.data.entity_id in state_attr('group.all_lights_only','entity_id') and
           trigger.event.data.entity_id.split('.')[1] not in skip_list }}
  action:
    - service: python_script.summary

whats even better, this automation seems to be much lighter on the system than the one in which all lights are defined separately. Enableing that one causes many timing issues, and even disconnects from the Hue hub, the biggest issue in my setup for the last few monthsā€¦

hope that too can be solved by this much more elegant and robust automation. (have several rebuilds to do now, cause there are more groups being checked in the setup, that can be rewritten in this way)
thx!

btw since Light is a valid domain, see Events - Home Assistant, wouldnā€™t this be possible:

platform: event
event_type: call_service
event_data:
  domain: light
  service: ['turn_on', 'turn_off']

or some valid service_template, or even leave out the service, since any call_service to the domain light would be ok to trigger the automation

- alias: Call service event (Light)'
  id: 'Call service event (Light)'
  trigger:
    platform: event
    event_type: call_service
    event_data:
      domain: light
  condition:
    - condition: template
      value_template: >
        {% set skip_list = ['group_for_wakeup', 'group_for_wakeup_2', 'entertainmentruimte_2', entertainmentruimte_1] %}
        {{ trigger.event.data.entity_id.split('.')[1] not in skip_list }}
  action:
    - service: python_script.summary

this works perfectly! letting @petro know too, since we had a previous attemptā€¦, which at least is solved now. Cool.

hiccup: lights that are turned on by the hue motion sensors and tradfri remotes donā€™t trigger the automation. Apparently that is not a service call.
To keep things tidy, I might have to use the event state_changed after all. Interesting this is.

if i would like to check more than 1 group, how would i need the change the state_attr(ā€˜group.all_lights_onlyā€™,ā€˜entity_idā€™) ?

With my python script Summary I see to these groups:

groups = ['group.family',
          'group.hubs_binary_pinged',
          'group.critical_devices_state',
          'group.media_player_media',
          'group.device_tracker_media',
          'group.all_lights_only',
          'group.iungo_switch_switches_template',
          'group.iungo_switch_appliances_template',
          'group.binary_sensors_active_template']

any change in these should trigger the automation to trigger the python script. So id need something like:

  condition:
    - condition: template
      value_template: >
           {% set groups = ['group.family',
              'group.hubs_binary_pinged',
              'group.critical_devices_state',
              'group.media_player_media',
              'group.device_tracker_media',
              'group.all_lights_only',
              'group.iungo_switch_switches_template',
              'group.iungo_switch_appliances_template',
              'group.binary_sensors_active_template'] %}
           {{ trigger.event.data.entity_id in state_attr('[groups]','entity_id') }}

obviously the above is incorrect, what would be the correct syntax to check for entities in more than 1 group using this setup and write the following automation in a more intelligent short-handā€¦

- alias: 'Call state change event (all)'
  id: 'Call state change event (all)'
#  hide_entity: True
#  initial_state: 'on'
  trigger:
    platform: event
    event_type: state_changed
  condition:
    - condition: template
      value_template: >
        {{ trigger.event.data.entity_id in state_attr('group.family','entity_id') or
           trigger.event.data.entity_id in state_attr('group.hubs_binary_pinged','entity_id') or
           trigger.event.data.entity_id in state_attr('group.critical_devices_state','entity_id') or
           trigger.event.data.entity_id in state_attr('group.media_player_media','entity_id') or
           trigger.event.data.entity_id in state_attr('group.device_tracker_media','entity_id') or
           trigger.event.data.entity_id in state_attr('group.all_lights_only','entity_id') or
           trigger.event.data.entity_id in state_attr('group.iungo_switch_switches_template','entity_id') or
           trigger.event.data.entity_id in state_attr('group.iungo_switch_appliances_template','entity_id') or
           trigger.event.data.entity_id in state_attr('group.binary_sensors_active_template','entity_id') }}
  action:
    service: python_script.summary

@pnbruckner and @petro, may I please ask to have another look at this?
thx,
Marius

trying the same for this automation:

- alias: 'Forward notifications to filed_notifications'
  id: 'Forward notifications to filed_notifications'
  initial_state: 'on'
  trigger:
    platform: event
    event_type: call_service
    event_data:
      domain: notify
      service: notify
  condition:
  action:
    service: notify.filed_notifications
    data_template:
      message: >
        {% set message = trigger.event.data.service_data.message %}
           {{as_timestamp(trigger.event.data.service_data.message.last_updated)|timestamp_local }}: {{ message }}

Im getting an error I cant seem to solve:
[homeassistant.helpers.service] Error rendering data template: UndefinedError: 'str object' has no attribute 'last_updated'

the thing is I dont know how to check for attributes or even entity_ids, since they are created dynamically when the automation runsā€¦
Ive also tried last_triggered, and .attributes.last_triggered, but they are not available.

this is what works for the automations notification, but is uses new_state as check, and that is not available when checking for call_service? :

     {{ as_timestamp(trigger.event.data.new_state.last_updated)|timestamp_local }}: {{ trigger.event.data.new_state.name }}

Using no template for time, and the timestamp = true in the sensor settings again shows the etc time, which is off 2 hours, a before. Could you please see if youā€™d know the correct template?

btw using {{ as_timestamp(now()) | timestamp_custom("%d %b %X") }}: {{ message }} now as a kind of general fall-back option.

thx,
Marius

Well, you can start by looking here.

And for events, you can search the HA log for details, although Iā€™ll grant you the way they are printed it takes a bit of interpretation. E.g.,:

pi@raspberrypi:/home/homeassistant/.homeassistant $ grep call_service home-assistant.log | tail -n 1
2018-07-25 07:30:11 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: service=select_option, service_data=entity_id=['input_select.period_of_day'], option=Day, service_call_id=1975497552-107, domain=input_select>

But in this case, why donā€™t you just use now() for the log timestamp?

That can be even a bit simpler:

{{ now().strftime('%d %b %X') }}: {{ message }}

even better. thx.

sure I study the templates documentation ā€¦ its just not as well documented on these events as I need it to be. Will try to go through the logs with even more detail.

and about this:

that seems not to be the case, as I tried that, and the same UTC timestamp was printed.

Well, that doesnā€™t make sense. Hereā€™s the code, look for yourself. The default for adding a timestamp is false:

hmm. can only say will test againā€¦ and report back.

youā€™re right, both are showing fine now, with the correct timestamp:

25 Jul: 17:39:10: Sense state changed (Light)
25 Jul: 17:39:35: Sense Active change
25 Jul: 17:40:10: Sense state changed (Light)