[SOLVED] How do I format a blueprint variable for use in a template trigger

blueprint:
  name: View Assist - Control display template REWORK
  description: This is used as a base for setting up display control on a View Assist
    Satellite (View Assist control v 1.2.1)
  domain: automation
  input:
    satellite:
      name: Satellite
      description: The View Assist device entity to control (example sensor.viewassist_living_room)
      selector:
        entity:
          filter:
          - domain:
            - sensor
          multiple: false
- id: mediaplayerchange
  platform: template
  value_template: >
    {% set ent = state_attr(!input satellite,
    'mediaplayer_device') %} {{ state_attr(ent, 'is_volume_muted') | bool }}
  alias: Mediaplayer muted
variables:
  satellite: !input satellite
  dashboard: !input dashboard
  display: !input display
  home: !input home
  music: !input music
  micunmute: !input micunmute
  micdevice: !input micdevice
  intent: !input intent
  intent_view: !input intent_view

I am trying to create a blueprint that uses the input in a template trigger. I am unable to figure it out and hope that someone can help. I get an error when using the code above. How can I use the input variable within the template? Note I’ve only included the relevant portion as this blueprint is very long

Thanks!

Hi dinki,

You use it like any other !input. If the trigger field will accept the !input, use it directly (I think entity’s are this way).
If the field will accept a template, use a trigger_variable: key section to convert the !input to a variable and use that in the trigger.

Hey friend. Thanks for the speedy reply. I did try:

trigger_variables:
  var_satellite: !input satellite

and modified the trigger like:

- id: mediaplayerchange
  platform: template
  value_template: >
    {% set ent = state_attr(var_satellite,
    'mediaplayer_device') %} {{ state_attr(ent, 'is_volume_muted') | bool }}
  alias: Mediaplayer muted

And it did not work. I had also tried:

- id: mediaplayerchange
  platform: template
  value_template: >
    {% set ent = state_attr(satellite,
    'mediaplayer_device') %} {{ state_attr(ent, 'is_volume_muted') | bool }}
  alias: Mediaplayer muted

before the above change as it looked like the variables were being defined as a variables block. I hope that I am just doing something silly but I can’t find it.

Maybe you removed it so as to “only included the relevant portion”, but there is no trigger key…

Overall, I think you’re going to run into reliability issues with a trigger like that. For example, if the value of ent changes, but both the “from ent” and “to ent” had a value of true for is_volume_muted, the trigger will not fire.

Full transparency, here’s the whole BP. Note I haven’t added the actions for these yet as I am trying to ensure the triggers work.

https://dpaste.org/T83KU

The value of ent will not change. That is part of the blueprint input. The automation I am converting into the blueprint works great so I have confidence that this will work if I can just figure out what I am doing wrong in the conversion to blueprint.

This is actually an addition to an existing blueprint to add additional functionality as I refactor the way I am doing things.

Thanks for your input as well. Hopefully seeing everything will make things clearer.

EDIT and this is the automation I’m using for the additions:

https://dpaste.org/Pgjpi

Note that in the automation it has this type:

triggers:
  - trigger: state
    entity_id:

I did convert these to this style:

- id: mediaplayerchange
  platform: state

Is that the problem?

Well that I know won’t work. !inputs are not variables.

Try refactoring and just put that all in trigger_variables. You gain then that the result of that boolean will be in the trace for you to see, and it’s cleaner. plus it rules out some of the weirdness in the trigger itself.
Just have the trigger react to the boolean result.

Thanks for the tip. I am trying that now but I am still doing something wrong as the automation is showing red and disabled when I have this:

trigger_variables:
  var_satellite: !input satellite
  var_mediaplayermuted: >-
    {% set satellite = !input satellite %}
    {% set ent = state_attr(satellite, 'mediaplayer_device') %} 
    {{ not state_attr(ent, 'is_volume_muted') | bool }}

I added the mediaplayermuted but I am still unsure how to pull that input variable into the jinja. Also, I am unsure when is appropriate to use > and when I need >- can you explain?

I think I’m close but not knowing where to make the change has got me running in circles.

EDIT:

trigger_variables:
  satellite: !input satellite
  var_mediaplayermuted: >-
    {% set ent = state_attr(satellite, 'mediaplayer_device') %} 
    {{ not state_attr(ent, 'is_volume_muted') | bool }}

This should work right? Had a typo above. Another question, I am editing this blueprint on disk. How do I ensure a reload? I have been reloading automations but that doesn’t seem to always work. Do I have to full restart?

Apologies for posting as I discover things. I thought I was getting somewhere with the variable setting:


trigger_variables:
  trigger_satellite: !input satellite
  trigger_mediaplayermuted: >-
    {% set target_device = state_attr(trigger_satellite, 'mediaplayer_device') %} 
    {{ state_attr(target_device, 'is_volume_muted') | bool }}
  trigger_mediaplayerunmuted: >-
    {% set target_device = state_attr(trigger_satellite, 'mediaplayer_device') %} 
    {{ not state_attr(target_device, 'is_volume_muted') | bool }}
  trigger_micmuted: >-
    {% set target_device = state_attr(trigger_satellite, 'mic_device') %}   
    {% if '_stt' in target_device %}
      {% set target_device = target_device | replace('sensor', 'switch') | replace('_stt', '_mic') %}
    {% endif %}
    {{ not states(target_device) | bool }}    
  trigger_micunmuted: >-
    {% set target_device = state_attr(trigger_satellite, 'mic_device') %}   
    {% if '_stt' in target_device %}
      {% set target_device = target_device | replace('sensor', 'switch') | replace('_stt', '_mic') %}
    {% endif %}
    {{ states(target_device) | bool }}

Unfortunately I am seeing this in the logs:

Error rendering trigger variables: TemplateError: Use of ‘state_attr’ is not supported in limited templates

Is there anything I can do about this?

EDIT removed Petro at mention. Apologies.

Perhaps what I am doing is not possible. For clarity, I have a template device that stores different information including things like the media_player and microphone device associated with this template device.

What I am trying to do with the blueprint is to set a trigger that checks if the microphone and/or the media_player gets muted and then to do something. I have this working in a proof of concept automation. The template sensor device is hard coded. For example:

  - id: mediaplayerchange
    trigger: template
    value_template: >
      {% set ent = state_attr('sensor.viewassist_masterbedroom',  'mediaplayer_device') %}
      {{ not state_attr(ent, 'is_volume_muted') | bool }}

Works great. I have a blueprint that does various things. The user is required to install this blueprint on a per device basis. So device ‘sensor.viewassist_masterbedroom’ would have this blueprint and the satellite name is one of the input fields. I am trying my hardest to get that input satellite name in to replace the ‘sensor.viewassist_masterbedroom’ in the example above so that this blueprint can be used for each device and add the additional functionality.

What I am unsure of, besides if it is possible, is how the blueprint works. Does it do what I hope and substitute those inputs into the automation where they are defined? If so, this should work right?

When I take control and look at yaml, I am seeing this (pulling out relevant parts):

trigger_variables:
  trigger_satellite: sensor.viewassist_masterbedroom
triggers:
  - alias: Mic muted
    id: micchange
    value_template: |-
      {% set target_device =  state_attr(trigger_satellite, 'mic_device') %}
        {% if '_stt' in target_device %}
          {% set target_device = target_device | replace('sensor', 'switch') | replace('_stt', '_mic') %}
        {% endif %}
      {{ not states(target_device) | bool }}
    trigger: template
  - alias: Mic unmuted
    id: micchange
    value_template: |-
      {% set target_device =  state_attr(trigger_satellite, 'mic_device') %}
        {% if '_stt' in target_device %}
          {% set target_device = target_device | replace('sensor', 'switch') | replace('_stt', '_mic') %}
        {% endif %}
      {{ states(target_device) | bool }}
    trigger: template

To me this should work just like the proof of concept automation where I am hardcoding the sensor name. I am not seeing the blueprint version triggering when the mic changes to mute.

This works fine in dev tools → template

      {% set trigger_satellite = "sensor.viewassist_masterbedroom" %}
      {% set target_device =  state_attr(trigger_satellite, 'mic_device') %}
        {% if '_stt' in target_device %}
          {% set target_device = target_device | replace('sensor', 'switch') | replace('_stt', '_mic') %}
        {% endif %}
      {{ not states(target_device) | bool }}

Full blueprint as it stands is here:
https://dpaste.org/JvBMq

I apologize profusely for dragging you guys through this with me. It seems that it IS working but I was not seeing the trigger because I had not set the action. I found this when I was not in the take control mode but just on the automation list screen where I was seeing the automation actually showing that the automation was ‘run’ with the ‘now’ time frame. It’s weird but working now. I’ll mark as solved.

I was literally gone all day and just got here. Post all you want. This will help others.
I’m glad you are going, and I think you will like the calcs in the variables section better. I have done this to many of my things.

It helps to use the solution tag somewhere as well.

1 Like