Scripts: modes and events

I want to trigger a template sensor every time a particular script is runs.

If my script uses mode: queued does a call_service event get fired every time the script starts again?

Or is it fired only once when it is first called?

I assume in the case of mode: parallel it is fired every time it is called?

In a nutshell I want one or more trigger sensors to update themselves so that every time the script runs they have the latest state.

I think the answers are yes and yes. Depending on conditions that is.

1 Like

Thanks.


I’m assuming you mean:

yes to:

and (therefore) no to:

and yes to:

And…

I am testing this but cannot get this trigger to, erm, trigger.
Clearly I have something wrong…

template:
  - trigger:
      - platform: event
        event_type: call_service
        event_data:
          domain: script
          service: turn_on
          service_data:
            - entity_id: script.test_tts
    sensor:
      - name: Test Sensor
        state: >
          {{ ["Triggered",
              "Updated",
              "New State"] | random }}

Any ideas?

May I ask how did you turn_on the script? Are you calling the service of script.turn_on or are you using the one from the UI / menu?

Just a thought, but I think you need to use script.turn_on.

if an automation is firing the script, just use automation.trigger. Otherwise the called service will be the script name or script.turn_on.

      - platform: event
        event_type: call_service
        event_data:
          domain: script
          service: turn_on
          service_data:
            - entity_id: script.test_tts
      - platform: event
        event_type: call_service
        event_data:
          domain: script
          service: test_tts

That didn’t work either.

I’d really rather not use the automation.trigger as the script is called from tons of different places. Not only would that be a pain to change but also having the trigger in the sensor makes it all much more scalable and maintainable (IMO)


This is what I am testing with in case you have the time/inclination to try it for yourself.
(I added the third trigger for good measure and out of desperation :wink: )

I expected the persistent notification to change (almost) every time script.test_event_trigger runs which I am doing from the Developer Tools, Services page. But it stays as ‘unknown’.

image

script:
  test_event_trigger:
    sequence:
      service: persistent_notification.create
      data:
        message: "{{ states('sensor.test_event_trigger') }}"


template:
  - trigger:
      - platform: event
        event_type: call_service
        event_data:
          domain: script
          service: turn_on
          service_data:
            - entity_id: script.test_event_trigger

      - platform: event
        event_type: call_service
        event_data:
          domain: script
          service: test_event_trigger

      - platform: event
        event_type: call_service
        event_data:
          domain: script
          service: script.test_event_trigger

    sensor:
      - name: Test Event Trigger
        state: >
          {{ ["Triggered",
              "Updated",
              "New State"] | random }}

Well that’s your problem, that’s invalid yaml

service: script.turn_on
data:
  entity_id: script.test_tts

what you’re sending is invalid json and it should result in an error because it doesn’t see the word entity_id as a string

So, try it with

service: script.turn_on
data:
  entity_id: script.test_tts

or

service: script.test_tts
service: script.turn_on
target:
  entity_id: script.test_event_trigger

If you choose the target from the UI (drop-down options), the YAML looks like the above.

Fair point.
But…

I am not sure that is the issue.

And the ‘correct’ way

They both seem to work without error (even though I agree the first one shouldn’t)
Neither trigger the sensor.

what errors are in the log not sure what ‘unknown’ is supposed to mean on that notification

I see test_event_trigger and your script name test_tts don’t match

Yeah, I’m really sorry that’s my fault that they don’t match. It was just as a result of simplifying the code for posting here and then running the original.

Everything I said still stands though

This works

service: script.test_event_trigger

These don’t work

service: script.turn_on
data: 
  entity_id: script.test_event_trigger

service: script.turn_on
data: {entity_id: script.test_event_trigger}

service: script.turn_on
data: {"entity_id": "script.test_event_trigger"}

I must still have something wrong in the trigger that uses the turn_on service.
Which is where I started.


Here is the actual code I ran to test

script:
  test_event_trigger:
    sequence:
      service: persistent_notification.create
      data:
        notification_id: >
          {{ now() | string }}
        title: Script test_event_trigger called.
        message: >
          At {{ now().hour }}:{{ now().hour }}:{{ now().second }} the state of 'sensor.test_event_trigger' is:
          
          {{ states('sensor.test_event_trigger') }}


template:
  - trigger:
      - platform: event
        event_type: call_service
        event_data:
          domain: script
          service: turn_on
          service_data:
            - entity_id: script.test_event_trigger

      - platform: event
        event_type: call_service
        event_data:
          domain: script
          service: test_event_trigger

    sensor:
      - name: Test Event Trigger
        state: >
          {{ ["Triggered",
              "Updated",
              "New State"] | random }}

Can you try modifying the template trigger sensor as below?

template:
  - trigger:
      - platform: event
        event_type: call_service
        event_data:
          domain: script
          service: turn_on
          service_data:
            entity_id:
              - script.test_event_trigger
    sensor:
      - name: Test Event Trigger
        state: >
          {{ ["Triggered",
              "Updated",
              "New State"] | random }}

Yes I can but no, I’m afraid that didn’t work either.

Works for me, with your “actual” code.

image
image

I suspect it’s pure luck, though.
Your notification and the actual state change of the sensor run in parallel. If the notification runs before the sensor is actually updates, you’ll get unknown

Yes but run it more than once and it should show the updated state from the previous trigger/run.

And in any case,

Are you sure?
How are you calling the script?

The template in the ‘actual code’ has two triggers.


Using this works (every time)

service: script.test_event_trigger


Using this doesn’t work.

service: script.turn_on
data: 
  entity_id: script.test_event_trigger

It’s clearly working because you’re getting the notification, it’s just that your template sensor isn’t populated in time for your script to have the info. So just build an automation that sends the message when the template is updated.

But if that was the case then the sensor would be populated (late) but running the script a second time would show the state that it was populated with from the first run, and then get updated (late) again.
Wouldn’t it?

It doesn’t. It stays forever ‘unknown’

In any case, today it came to me that there is a much simpler way to achieve what I want to achieve

  - trigger:
      - platform: state
        entity_id: script.test_script_turn_on
        attribute: current

My sensor(s) now seem to be updated every time the script runs irrespective of how it is called or the mode it is running in.