Building a string from values imported from Home Assistant

I have 3 pieces of data I’m pulling in from Home Assistant using current state:

  • input_text.node_current_status (this is a text value, eg., “Inside”)
  • input_text.node_previous_status (this is a text value, eg., “Outside”)
  • sensor.update_minutes_ago (this is a number in minutes, eg., 5)

How do I build these into a string so that I can have TTS announce “The dog is inside, but it was outside 5 minutes ago”

I’m stumped on how to get this to work. I’ve tried storing each of the values into flow variables using the function tool

flow.set("current_status",msg.data);
return msg;

flow.set("status_previous",msg.data);
return msg;

flow.set("update_minutes_ago",msg.data);
return msg;

and then putting these together

var current_status =  flow.get("current_status");
var update_minutes_ago = flow.get("update_minutes_ago");
var status_previous = flow.get("status_previous");

msg.payload = 
{
  "data": 
  {
    "message": "The dog is " + current_status + ". " + update_minutes_ago + " minutes ago he was " + status_previous
  }
}
return msg;

Help appreciated! I’m clearly looking at this the wrong way :grinning:

If you’re using a call-service node to tell Home Assistant to do the TTS you can just form it all inside the data field of the call-service node.

The dog is {{states.input_text.node_current_status}}. {{states.sensor.update_minutes_ago}} minutes ago he was {{states.input_text.node_previous_status}}

If you’re wanting to pass it to a different node then using a couple of current-state nodes and a template node should accomplish the task.

[{"id":"373a2ab4.29c286","type":"inject","z":"ffbd7f06.4a014","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":144,"y":480,"wires":[["9646d923.432558"]]},{"id":"9646d923.432558","type":"api-current-state","z":"ffbd7f06.4a014","name":"node_current_status","server":"2dad33ee.42bf5c","outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_text.node_current_status","state_type":"str","state_location":"node_current_status","override_payload":"msg","entity_location":"","override_data":"none","x":324,"y":480,"wires":[["d92bf2f0.d578"]]},{"id":"d92bf2f0.d578","type":"api-current-state","z":"ffbd7f06.4a014","name":"node_previous_status","server":"2dad33ee.42bf5c","outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_text.node_previous_status","state_type":"str","state_location":"node_previous_status","override_payload":"msg","entity_location":"","override_data":"none","x":548,"y":480,"wires":[["ec9f0147.f77b2"]]},{"id":"ec9f0147.f77b2","type":"api-current-state","z":"ffbd7f06.4a014","name":"update_minutes_ago","server":"2dad33ee.42bf5c","outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sensor.update_minutes_ago","state_type":"str","state_location":"update_minutes_ago","override_payload":"msg","entity_location":"","override_data":"none","x":772,"y":480,"wires":[["67634f6c.20eb7"]]},{"id":"3e380017.db9a8","type":"debug","z":"ffbd7f06.4a014","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1110,"y":480,"wires":[]},{"id":"67634f6c.20eb7","type":"template","z":"ffbd7f06.4a014","name":"format it","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"The dog is {{node_current_status}}. {{update_minutes_ago }} minutes ago he was {{node_previous_status}}","output":"str","x":956,"y":480,"wires":[["3e380017.db9a8"]]}]
1 Like

Thanks Kermit - that’s perfect!

I appreciate that both examples you’ve given AND that they’re both simple in execution.

I’ll be sure to use these as a basis of other Node Red projects in the future

:smiley::smiley::smiley:

Okay, so now I feel like more of a dumb-dumb.

How do I parse the resulting string “The dog is in the Unknown. 14 minutes ago he was in the --” seeing that it is using the Mustache template (which I’m not familiar with)

Talk about handholding :grimacing:

For the ‘make announcement’ node make the data field {"message": "{{payload}}"}

1 Like

Thanks Kermit - it works :grinning: