Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 103335

I need some help with my script (noob warning)

$
0
0

i have a script that handles how my alexa speakers makes announcements.
i call the script from my automations and can supply variables to the script so it knows which alexa device to play the notification and sets the speaker volume for the notification.

in my automation i call the service to run the script like so:

service: script.alexa_announce_gate_left_open
data:
  speaker_device: media_player.echo_show
  speaker_service: notify.alexa_media
  announce_volume: 0.4

i’m trying to improve the script further by adding a room occupancy variable so that the announcement plays if someone is in that room. if there was nobody it waits until someone enters the room unless the alarm was already cleared.

so i added a condition in the script that checks the state of the particular speaker’s room occupancy boolean.
the script works fine if I use the actual boolean name ie input_boolean.room_occupancy_study, but I need that to be ‘{{ speaker_room }}’. However I get the error Message malformed: Entity {{ speaker_room }} is neither a valid entity ID nor a valid UUID for dictionary value @ data[‘sequence’][1][‘if’][0][‘entity_id’]

for a start i was going to have speaker_room as another data variable that the automation passes to the script, but i eventually want to make it more advanced by deriving the correct speaker_room value based on the speaker that is being called. maybe that could be done using a bunch of if/else statements? i haven’t figured out that part yet. i’m taking baby steps.

i’m really not programmer so whatever i’ve done is from googling and copy-pasting. but i’m quite happy with what i could achieve.

here’s the script that i’ve come up with. any pointers would be very much appreciated!

alias: Alexa Announce Gate Left Open
sequence:
  - variables:
      gate_open_msg: Someone didn't close the gate
      old_volume: '{{ state_attr(speaker_device,''volume_level'') | float }}'
  - if:
      - condition: state
        entity_id: '{{ speaker_room }}'
        state: 'on'
    then:
      - service: media_player.volume_set
        data:
          volume_level: '{{ announce_volume }}'
        target:
          entity_id: '{{ speaker_device }}'
      - service: '{{ speaker_service }}'
        data:
          data:
            type: tts
          message: '{{ gate_open_msg }}'
          target: '{{ speaker_device }}'
      - service: media_player.volume_set
        data:
          volume_level: '{{ old_volume }}'
        target:
          entity_id: '{{ speaker_device }}'
    else:
      - wait_for_trigger:
          - platform: state
            entity_id:
              - '{{ speaker_room }}'
            to: 'on'
      - condition: state
        entity_id: binary_sensor.zone_gate_open
        state: 'on'
      - service: media_player.volume_set
        data:
          volume_level: '{{ announce_volume }}'
        target:
          entity_id: '{{ speaker_device }}'
      - service: '{{ speaker_service }}'
        data:
          data:
            type: tts
          message: '{{ gate_open_msg }}'
          target: '{{ speaker_device }}'
      - service: media_player.volume_set
        data:
          volume_level: '{{ old_volume }}'
        target:
          entity_id: '{{ speaker_device }}'
mode: parallel
max: 5

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 103335

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>