I am trying to get HA to play differing Sonos radio streams depending on what time of day it is. I want to use Templates but this is the first time I have tried using them in anger.
Here is the automation (I still need to add variables depending on what time of day it is!). When someone comes home, when the Sonos in the living room isnt playing and the TV is off and between 09:01 and 18:30 run the script with the template variable:
alias: 'Family home play Sonos depending on time of day'
trigger:
- platform: state
entity_id: group.family
from: 'not_home'
to: 'home'
condition:
condition: and
conditions:
- condition: state
entity_id: media_player.living_room
state: 'paused'
- condition: state
entity_id: media_player.samsung_tv
state: 'off'
- condition: time
after: '09:01:00'
before: '18:30:00'
weekday:
- mon
- tue
- wed
- thu
- fri
action:
service: script.turn_on
entity_id: script.sonos_template
data_template:
variables: >-
{set sonos = "Absolute Radio"}
source: {{ sonos }}
This then calls the following script, which groups all my Sonos, sets the volume and plays the favourite stream I defined in the automation:
I am doing something wrong as nothing happens and I get this error in the log
homeassistant.core: Invalid service data for script.turn_on: expected dict for dictionary value @ data['variables']. Got '{set sonos = "Absolute Radio"} source:'
Do I need to define this variable somewhere? I know the trigger and condiitons are all working, but I am not understanding the use of templates here. Can anyone help? Thanks
OK so it fires script.sonos_template and groups the sonos and sets the volume but doesnt play anything and I get database errors in the log?? Not sure if they are related.
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) database is locked [SQL: 'SELECT recorder_runs.run_id AS recorder_runs_run_id, recorder_runs.start AS recorder_runs_start, recorder_runs."end" AS recorder_runs_end, recorder_runs.closed_incorrect AS recorder_runs_closed_incorrect, recorder_runs.created AS recorder_runs_created \nFROM recorder_runs \nWHERE recorder_runs.start < ? AND recorder_runs."end" > ?\n LIMIT ? OFFSET ?'] [parameters: ('2017-01-11 20:13:14.115157', '2017-01-11 20:13:14.115157', 1, 0)]
17-01-12 20:13:35 homeassistant.components.recorder: Error during query: (sqlite3.OperationalError) database is locked
17-01-12 20:13:40 homeassistant.components.recorder: Error during query: (sqlite3.OperationalError) database is locked
You are referring to it as sonos_template in the automation so you need to name it that or find out what the name is (it is most likely play_absolute_radio) by going to the states page ( < > in the side bar) and finding the script in the list. You’ll want to refer to it as it appears there. If you add the line above alias you can name it specifically.
ah, that’s fair. A difference between my setup and something I did not have experience with. for reference I have a .yaml called sonos_tts.yaml but inside is:
sonos_say:
alias: SONOS TTS
sequence:
- service: media_player.sonos_snapshot
data_template:
entity_id: "{{ where }}"
- service: tts.google_say
data_template:
entity_id: "{{ where }}"
message: "{{ what }}"
- service: media_player.sonos_restore
data_template:
entity_id: "{{ where }}"
To call that script, I use script.sonos_say.
another piece that may be missing from your script, since in the data section we are using a template to include a variable in the ‘section’ you will need to adjust it to use a data_template:
I would like to factor this sensor into the playing of a stream. So I changed the action statement in the automation to play the stream only during the ‘morning’ or ‘afternoon’. Unfortunately this doesn’t work but does not error out. Any idea what I am doing wrong with my template syntax?
in your prior test/example, you included an entity_id but you set it statically in the script so it wasn’t necessary. If you wanted the automation to also set which media_player you want to use, it would look like:
action:
service: script.turn_on
entity_id: script.sonos_template
data_template:
variables:
entity_id: {{ LOGIC TO SET ENTITY ID - you can then reference this inside the script }}
source: >
{% if is_state('sensor.time_of_day', 'morning') or is_state('sensor.time_of_day', 'afternoon') %}
'Absolute Radio'
{% endif %}
Thanks for your continued help. The stream still doesn’t fire. It definitely calls the script as the speakers group and volume sets but nothing else happens. Really odd.
Yeah already tried that. I think this might be a bug as hass --script check_config doesnt report any issues. I think it simply doesn’t pass the data_template over to the script.
If I change it to the following (have bypassed the ‘time of day’ sensor for now):