Is it possible? Include the script name in a notify message

Been round the boards for a couple of days with this one but can’t figure it out.

Short question: is it possible to include the script name in the body of a notify action.

Context: I have a series of WiFi plugs which jump on/off the network (another problem to sort out another day) so have been creating more robust scripts triggered by automation’s to handle these. Basically, if they don’t turn on at first attempt I’m trying again (and again) until finally giving up. When I give up, I’m pinging a message to my phone with the state of the devices so I can go and turn the thing off and back on (yes, I know I need better devices!).

So two things I’m sure people will point out - is this overkill, yes probably but important for me to be able to get to the device and sort it out before the family complains and block any further evolution of my master plan!

Secondly, why not just type the script name in Mike? Well yes, that’s obvious but this has now become my nemesis and would love if someone could crack it for, that way when I replicate this I simply paste it in with no edits.

The script is below for interest (or comment!) and the group in question contains 5 switches (and SCRIPT NAME is where I put the name of the script that’s running).

Thanks in advance guys and gals!

lights_kitchen_on:
  alias: Lights - Kitchen - On
  icon: hass:led-strip-variant
  mode: single
  sequence:
  - data: {}
    entity_id: group.kitchen
    service: homeassistant.turn_on
  - repeat:
      sequence:
      - delay: '20'
      - data: {}
        entity_id: group.kitchen
        service: homeassistant.turn_on
      while:
      - condition: template
        value_template: '{{ states|selectattr(''entity_id'',''in'',state_attr(''group.kitchen'',''entity_id''))
          | rejectattr(''state'',''eq'',''on'')|list|count > 0 }}'
      - condition: template
        value_template: '{{ repeat.index <= 15 }}'
  - condition: template
    value_template: '{{ states|selectattr(''entity_id'',''in'',state_attr(''group.kitchen'',''entity_id''))
      | rejectattr(''state'',''eq'',''on'')|list|count > 0 }}'
  - data:
      message: '{%- set ns = namespace(message = ''**SCRIPT NAME** failed to finish
        successfully, devices states are:\n'') -%} {%- for item in states.group.kitchen.attributes.entity_id
        -%} {%- set ns.message = ns.message + '' - '' + item + '': '' + states(item)
        + ''\n'' -%} {%- endfor -%} {{ ns.message }}'
    service: notify.mobile_app_mike_wood_iphonex

FYI, this:

    value_template: '{{ states|selectattr(''entity_id'',''in'',state_attr(''group.kitchen'',''entity_id''))
      | rejectattr(''state'',''eq'',''on'')|list|count > 0 }}'

can be simplified to this:

    value_template: '{{ expand(''group.kitchen'') | rejectattr(''state'',''eq'',''on'')|list|count > 0 }}'

But regarding your main question, when you “replicate it”, what will be different in each one? My guess is the name of the group, yes? If so, how about:

lights_group_on:
  alias: Lights - Group - On
  icon: hass:led-strip-variant
  fields:
    group:
      description: Group object ID
      example: kitchen
  mode: single
  sequence:
  - data_template:
      entity_id: "group.{{ group }}"
    service: homeassistant.turn_on
  - repeat:
      sequence:
      - delay: '20'
      - data_template:
          entity_id: "group.{{ group }}"
        service: homeassistant.turn_on
      while:
      - condition: template
        value_template: '{{ expand(''group.''~group) | rejectattr(''state'',''eq'',''on'')|list|count > 0 }}'
      - condition: template
        value_template: '{{ repeat.index <= 15 }}'
  - condition: template
    value_template: '{{ expand(''group.''~group) | rejectattr(''state'',''eq'',''on'')|list|count > 0 }}'
  - data:
      message: '{%- set ns = namespace(message = ''Lights - ''~group~'' - On failed to finish
        successfully, devices states are:\n'') -%} {%- for item in states.group[group].attributes.entity_id
        -%} {%- set ns.message = ns.message + '' - '' + item + '': '' + states(item)
        + ''\n'' -%} {%- endfor -%} {{ ns.message }}'
    service: notify.mobile_app_mike_wood_iphonex

Then call it like this:

- service: script.lights_group_on
  data:
    group: kitchen
1 Like

Damn it, you think you’re getting a handle on all this then something comes along and shows you that you’re an amateur and only just beginning scratching the surface!!

Yup, exactly right. This is a dream and will let me consolidate down the number of scripts I’ve got to two - done a test and works out of the box. Plus the principle has now got me thinking and wondering where else I can streamline things!!

Thanks @pnbruckner, people like yourself make this so much more pleasurable and fun for others. Cheers! (right off to write some more scripts :slight_smile:)

Sorry @pnbruckner! Had only tested happy path - broke a few (physical) things to test the other routes through and back to getting an issue with the final bit above, can’t get this message to output.

I did wonder if:

{%- for item in states.group[group].attributes.entity_id -%}

Was missing a .(dot) between the group and [group] but tried that, also pondered if:

''~group~''

Shouldn’t have had the trailing ~ as this wasn’t included when the group was used in the value_template. But to no avail, still a little bit stuck, will continue to tinker but any thoughts welcome. Pulled the following error logs which I think relate to this!

Log Details (ERROR)

Logger: homeassistant.components.script.lights_generic_on
Source: helpers/script.py:906
Integration: Scripts (documentation, issues)
First occurred: 16:02:08 (10 occurrences)
Last logged: 16:38:19

  • Lights - Generic - On: Error executing script. Error rendering template for call_service at pos 4: UndefinedError: homeassistant.helpers.template.DomainStates object has no element Undefined
  • Lights - Generic - On: Error executing script. Invalid data for call_service at pos 5: invalid template (TemplateSyntaxError: expected token ‘,’, got ‘string’) for dictionary value @ data[‘message’]
  • Lights - Generic - On: Error executing script. Invalid data for call_service at pos 5: invalid template (TemplateSyntaxError: expected name or number) for dictionary value @ data[‘message’]
  • Lights - Generic - On: Error executing script. Error rendering template for call_service at pos 5: str: Invalid domain name ‘group_’
  • Lights - Generic - On: Error executing script. Error rendering template for call_service at pos 5: UndefinedError: homeassistant.helpers.template.DomainStates object has no element Undefined

You might try this:

  - data_template:
      message: >
        Lights - {{ group }} - On failed to finish successfully, devices states are:
        {% for state in expand('group.' ~ group) -%}
        - {{ state.entity_id }}: {{ state.state }}
        {% endfor %}
    service: notify.mobile_app_mike_wood_iphonex

FYI, ~ is a binary operator that converts its operands to strings and then concatenates them.

EDIT: If you want to see how this works, enter this into the Template editor:

{% set group = "kitchen" %}
Lights - {{ group }} - On failed to finish successfully, devices states are:
{% for state in expand('group.' ~ group) -%}
- {{ state.entity_id }}: {{ state.state }}
{% endfor %}
1 Like

Out of curiosity, what are the two? Is it “on” and “off”? If so, another variable can be added for that to make the one script do both.

Perfect, and thanks again for your help @pnbruckner

I had been trying to work it through on the template editor with the original but don’t quite have enough of a handle on it all at the moment (all major leaps forward have been through taking other peoples work I’ve found on the community posts and tweaking until I get the desired result!).

Yup, right again - on and off. After I had posted I got around to pondering if I could do this (grey matter takes a while to warm up) and figured it would be do’able…one for tomorrow though.

Thanks again! Marking as solved with thanks :slight_smile:

1 Like