Extracting status message and TTS from litter-robot

alias: Test - Litter Robot
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.litter_robot_status_code
condition: []
action:
  - service: tts.google_translate_say
    data:
      entity_id: media_player.aaron_s_speakers
      message: [litterrobot__status_code message here]
      cache: true
mode: single

I wanted to create a TTS automation that would trigger every time the [sensor.litter_robot_status_code] changes. The trigger works, however, I need to figure out how to do the TTS part. On the status codes linked above, there are 25 different status it can get. Instead of making 25 automations, I am seeking guidance if there is a way to grab the current status code message (for example: “Clean Cycle Complete”), and just use that.

As I can understand the documentation, the sensor already outputs a state as a human readable value.

Status Code - sensor - Displays the status code (Clean Cycle in Progress, Ready, Drawer Full, etc).

If so, all you need is {{ trigger.to_state.state }} in your message.

Additionally, I would use an automation condition to avoid unwanted messages:


condition:
  - "{{ trigger.to_state.state != 'unavailable' and trigger.from_state.state != 'unavailable' }}"


(unless you want to be notified even if the sensor is not reachable)

So I tried this, and it worked, but it is saying the code and not the message itself.

image

I just had it come through and say “CCP” which is the code, but I want it to say the actual message in blue here. Is that possible?

service: tts.google_translate_say
data:
  entity_id: media_player.aaron_s_room_speaker_1
  message: "{{ trigger.to_state.state }}"

Can you please test it with

```

service: notify.persistent_notification
data:
message: “{{ trigger.entity_id }}: {{ trigger.to_state.state }}”

```

?

My post crossed with your editing.

Copy the following into Developer Tools ——> Template and tell me the result:


{% set status = states('sensor.litter_robot_status_code') %}
{% set code = {
      "br": "Bonnet Removed",
      "ccc": "Clean Cycle Complete",
      "ccp": "Clean Cycle In Progress",
      "cd": "Cat Detected",
      "csf": "Cat Sensor Fault",
      "csi": "Cat Sensor Interrupted",
      "cst": "Cat Sensor Timing",
      "df1": "Drawer Almost Full - 2 Cycles Left",
      "df2": "Drawer Almost Full - 1 Cycle Left",
      "dfs": "Drawer Full",
      "dhf": "Dump + Home Position Fault",
      "dpf": "Dump Position Fault",
      "ec": "Empty Cycle",
      "hpf": "Home Position Fault",
      "off": "[%key:common::state::off%]",
      "offline": "Offline",
      "otf": "Over Torque Fault",
      "p": "[%key:common::state::paused%]",
      "pd": "Pinch Detect",
      "pwrd": "Powering Down",
      "pwru": "Powering Up",
      "rdy": "Ready",
      "scf": "Cat Sensor Fault At Startup",
      "sdf": "Drawer Full At Startup",
      "spf": "Pinch Detect At Startup"
    } %}



{{ code[status] if status in code.keys() else 'no status' }}

image

this is interesting. can you explain what this is that you had me put in? its a bit foreign to me but i would like to learn it.

The code variable represents a dictionary, in it there are key-value-pairs (e.g. “cst” stands for “Cat Sensor Timing”). B we need to identify the right pair (and so the wanted output). This we do by comparing the state of your sensor with existing items in the dictionary using {{ code[status] }} To avoid a bad result, we combine it with a condition: If there is no existing key in the dictionary that matches the state of your sensor = if status in code.keys() then return 'no status' .

My English is miserable, I hope you can follow.

P.S.: Maybe it is getting clearer by copying these code snippets additionally into Developer Tools:


{{ code[status] }}

{{ code.keys() }}

{{ code.values() }}

Do I need to copy that code dictionary into my automation now to get it to work?

You can put it in your message field:


alias: Test - Litter Robot
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.litter_robot_status_code
condition: []
action:
  - service: tts.google_translate_say
    data:
      entity_id: media_player.aaron_s_speakers
      message: |-
        {% set status = trigger.to_state.state %}
        {% set code = … %}
        {{ code[status]|replace("[%key:common::state::paused%]", "paused") |replace("[%key:common::state::off%]", "off") if status in code.keys() else 'no status' }}
      cache: true
mode: single

I’ve added to replace rules.

Sorry. Do not understand. What does “I’ve added to replace rules.” mean?

I don’t know how is this processed by tts:


"off": "[%key:common::state::off%]"

"p": "[%key:common::state::paused%]"

So I’ve added readable labels (off and paused):


{{ code[status]|replace("[%key:common::state::paused%]", "paused") |replace("[%key:common::state::off%]", "off")

Otherwise, it may be that your speaker says “[%key:common::state::off%]”

Gotcha, where do I put the dictionary from earlier in there? At which spot?

In the ‘code’ line


     message: |-
        {% set status = trigger.to_state.state %}
        {% set code = … %}

Okay, here is what I pasted in, and the error I get:

alias: Test - Litter Robot
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.litter_robot_status_code
condition: []
action:
  - service: tts.google_translate_say
    data:
      entity_id: media_player.aaron_s_speakers
      message: |-
        {% set status = trigger.to_state.state %}
        {% set code = 
      "br": "Bonnet Removed",
      "ccc": "Clean Cycle Complete",
      "ccp": "Clean Cycle In Progress",
      "cd": "Cat Detected",
      "csf": "Cat Sensor Fault",
      "csi": "Cat Sensor Interrupted",
      "cst": "Cat Sensor Timing",
      "df1": "Drawer Almost Full - 2 Cycles Left",
      "df2": "Drawer Almost Full - 1 Cycle Left",
      "dfs": "Drawer Full",
      "dhf": "Dump + Home Position Fault",
      "dpf": "Dump Position Fault",
      "ec": "Empty Cycle",
      "hpf": "Home Position Fault",
      "off": "[%key:common::state::off%]",
      "offline": "Offline",
      "otf": "Over Torque Fault",
      "p": "[%key:common::state::paused%]",
      "pd": "Pinch Detect",
      "pwrd": "Powering Down",
      "pwru": "Powering Up",
      "rdy": "Ready",
      "scf": "Cat Sensor Fault At Startup",
      "sdf": "Drawer Full At Startup",
      "spf": "Pinch Detect At Startup" 
      %}
        {{ code[status]|replace("[%key:common::state::paused%]", "paused") |replace("[%key:common::state::off%]", "off") if status in code.keys() else 'no status' }}
      cache: true
mode: single

image

It has to be the code you copied into Developer Tools.


      message: |-
        {% set status = trigger.to_state.state %}
        {% set code = {
           "br": "Bonnet Removed",
           "ccc": "Clean Cycle Complete",
            … and so on…
           } %}

oh. do i need to remove that out of there? i fixed the code and removed the developer template but still get that error

Looks your automation like that?


alias: Test - Litter Robot
description: ""
mode: single

trigger:
  - platform: state
    entity_id:
      - sensor.litter_robot_status_code
condition: []
action:
  - service: tts.google_translate_say
    data:
      entity_id: media_player.aaron_s_speakers
      message: |-
        {% set status = trigger.to_state.state %}
        {% set code = {
          "br": "Bonnet Removed",
          "ccc": "Clean Cycle Complete",
          "ccp": "Clean Cycle In Progress",
          "cd": "Cat Detected",
          "csf": "Cat Sensor Fault",
          "csi": "Cat Sensor Interrupted",
          "cst": "Cat Sensor Timing",
          "df1": "Drawer Almost Full - 2 Cycles Left",
          "df2": "Drawer Almost Full - 1 Cycle Left",
          "dfs": "Drawer Full",
          "dhf": "Dump + Home Position Fault",
          "dpf": "Dump Position Fault",
          "ec": "Empty Cycle",
          "hpf": "Home Position Fault",
          "off": "[%key:common::state::off%]",
          "offline": "Offline",
          "otf": "Over Torque Fault",
          "p": "[%key:common::state::paused%]",
          "pd": "Pinch Detect",
          "pwrd": "Powering Down",
          "pwru": "Powering Up",
          "rdy": "Ready",
          "scf": "Cat Sensor Fault At Startup",
          "sdf": "Drawer Full At Startup",
          "spf": "Pinch Detect At Startup"
        } %}
        {{ code[status]|replace("[%key:common::state::paused%]", "paused") |replace("[%key:common::state::off%]", "off") if status in code.keys() else 'no status' }}
      cache: true


Sorry, I don’t understand now. Because it’s literally the same as what you put, but now it saves? Really odd…

Your code


        {% set code = 
         "br": "Bonnet Removed",

My code


        {% set code = {
         "br": "Bonnet Removed",
         } %}
      

The dictionary has to be within { }

gotcha. that did it, and it looks like its working too.