Rememeber or Reuse previous state an reuse or reset it

Hi, next node-red question.

I would like to set and reset the “play state” of alexa media player to previous state. Thus if it was not playing it should not play the announcement was there. And vice versa.

like this (but this is thus not working :-):

Have you tried the scene service?

I prefer not to use that, This is a node-red portion of the forum and I prefer to do it there hence an answer there.

Thank you for your reply!

You can simply create a scene on the fly within NodeRed by simply calling the service. If you dont want to do that, then you need to use a function node, write the state to a variable and then once you want to restore, call the variable and set this state via the call service node.

yes, this sounds logical, but how do I do that? -->

  1. write the state to a variable?
  2. call the variable
  3. set state (via call service)

I am looking for an example as I am not a programmer…

You can follow this flow:

[{"id":"45dda31f.44616c","type":"function","z":"8a2eb1df.353f7","name":"Save Settings","func":"msg.volume = msg.data.attributes.volume_level\nreturn msg;","outputs":1,"noerr":0,"x":780,"y":1140,"wires":[["26d9e9d7.7e9766"]]},{"id":"491e7ba6.c0ae34","type":"function","z":"8a2eb1df.353f7","name":"Previous time","func":"newmsg = {}\n\nnewmsg.payload = { data: { \"volume_level\":msg.volume} }\n\nreturn newmsg;","outputs":1,"noerr":0,"x":1260,"y":1140,"wires":[["377b0661.7ed4ca"]]},{"id":"5e815aa5.268774","type":"server-state-changed","z":"8a2eb1df.353f7","name":"Call?","server":"2fba4297.e4145e","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"marcus.call","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":90,"y":1140,"wires":[["9d3cd515.aac2b8"],[]]},{"id":"9d3cd515.aac2b8","type":"api-current-state","z":"8a2eb1df.353f7","name":"","server":"2fba4297.e4145e","version":1,"outputs":2,"halt_if":"home","halt_if_type":"str","halt_if_compare":"is_not","override_topic":false,"entity_id":"person.marcus","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":280,"y":1140,"wires":[[],["69a01d54.d24614"]]},{"id":"69a01d54.d24614","type":"api-current-state","z":"8a2eb1df.353f7","name":"Spotify?","server":"2fba4297.e4145e","version":1,"outputs":2,"halt_if":"playing","halt_if_type":"str","halt_if_compare":"is_not","override_topic":true,"entity_id":"media_player.spotify","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":480,"y":1140,"wires":[[],["fdd013f4.b775c"]]},{"id":"eb04b83f.450f98","type":"comment","z":"8a2eb1df.353f7","name":"Volume Down when call active on phone","info":"","x":160,"y":1100,"wires":[]},{"id":"fdd013f4.b775c","type":"api-current-state","z":"8a2eb1df.353f7","name":"Volume?","server":"2fba4297.e4145e","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"media_player.spotify","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":620,"y":1140,"wires":[["45dda31f.44616c"]]},{"id":"faf031d7.6150a","type":"ha-wait-until","z":"8a2eb1df.353f7","name":"","server":"2fba4297.e4145e","outputs":1,"entityId":"marcus.call","entityIdFilterType":"exact","property":"state","comparator":"is","value":"off","valueType":"str","timeout":0,"timeoutType":"num","timeoutUnits":"seconds","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":1100,"y":1140,"wires":[["491e7ba6.c0ae34"]]},{"id":"377b0661.7ed4ca","type":"api-call-service","z":"8a2eb1df.353f7","name":"","server":"2fba4297.e4145e","version":1,"debugenabled":false,"service_domain":"media_player","service":"volume_set","entityId":"media_player.spotify","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1490,"y":1140,"wires":[[]]},{"id":"26d9e9d7.7e9766","type":"api-call-service","z":"8a2eb1df.353f7","name":"Set Volume","server":"2fba4297.e4145e","version":1,"debugenabled":false,"service_domain":"media_player","service":"volume_set","entityId":"media_player.spotify","data":"{\"volume_level\":\"0.05\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":950,"y":1140,"wires":[["faf031d7.6150a"]]},{"id":"2fba4297.e4145e","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

Or you can also save states to a file (so it survives a restart)

Example:

var newmsg = {}
global.set("alarm", 1, "storeInFile");
newmsg.payload = "True";

return newmsg;

Do you have an example of a node red flow creating a scene? I’m stuck on the correct formatting for the JSON.

Of course, please see below:

All you need is to set a name (Scene_ID) and define the Entity_IDs.

Example:

{"scene_id":"schlafzimmer_snapcast","snapshot_entities":"media_player.snapcast_schlafzimmer, media_player.snapcast_schlafzimmer_tv"}

Flow:

[{"id":"c8794526.36bc58","type":"api-call-service","z":"5b3a5364.173cdc","name":"Bett Mute","server":"2fba4297.e4145e","version":1,"debugenabled":false,"service_domain":"media_player","service":"volume_mute","entityId":"media_player.snapcast_schlafzimmer","data":"{\"is_volume_muted\":\"true\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":820,"y":1160,"wires":[[]]},{"id":"ae62a0fe.eec4","type":"api-call-service","z":"5b3a5364.173cdc","name":"TV Mute","server":"2fba4297.e4145e","version":1,"debugenabled":false,"service_domain":"media_player","service":"volume_mute","entityId":"media_player.snapcast_schlafzimmer_tv","data":"{\"is_volume_muted\":\"true\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":820,"y":1220,"wires":[[]]},{"id":"7455f425.27efbc","type":"api-call-service","z":"5b3a5364.173cdc","name":"Snapcast Scene Create","server":"2fba4297.e4145e","version":1,"debugenabled":false,"service_domain":"scene","service":"create","entityId":"","data":"{\"scene_id\":\"schlafzimmer_snapcast\",\"snapshot_entities\":\"media_player.snapcast_schlafzimmer, media_player.snapcast_schlafzimmer_tv\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":610,"y":1160,"wires":[["c8794526.36bc58","ae62a0fe.eec4"]]},{"id":"c405973c.6ce688","type":"api-call-service","z":"5b3a5364.173cdc","name":"Snapcast Scene On","server":"2fba4297.e4145e","version":1,"debugenabled":false,"service_domain":"scene","service":"turn_on","entityId":"scene.schlafzimmer_snapcast","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":860,"y":1400,"wires":[["b309626d.7d009"]]},{"id":"b309626d.7d009","type":"api-call-service","z":"5b3a5364.173cdc","name":"Licht Scene On","server":"2fba4297.e4145e","version":1,"debugenabled":false,"service_domain":"scene","service":"turn_on","entityId":"scene.schlafzimmer_licht","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1060,"y":1400,"wires":[[]]},{"id":"cde0e391.5a1e","type":"comment","z":"5b3a5364.173cdc","name":"Mute Snapcast if TV is ON","info":"","x":610,"y":1120,"wires":[]},{"id":"eaa9410.2c9efc","type":"api-call-service","z":"5b3a5364.173cdc","name":"Licht Scene Create","server":"2fba4297.e4145e","version":1,"debugenabled":false,"service_domain":"scene","service":"create","entityId":"","data":"{\"scene_id\":\"schlafzimmer_licht\",\"snapshot_entities\":\"light.bedroom_dresser,light.bedroom_left,light.bedroom_right,light.bedroom_tv_left,light.bedroom_tv_right\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":590,"y":1300,"wires":[["40347a37.fc2184"]]},{"id":"40347a37.fc2184","type":"api-call-service","z":"5b3a5364.173cdc","name":"Light Off","server":"2fba4297.e4145e","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.schlafzimmer","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":820,"y":1300,"wires":[[]]},{"id":"84131829.96b5e8","type":"comment","z":"5b3a5364.173cdc","name":"Turn of Light if TV is ON","info":"","x":600,"y":1260,"wires":[]},{"id":"47f4ec86.965c14","type":"comment","z":"5b3a5364.173cdc","name":"Turn on Light & Unmute Snapcast if TV is off","info":"","x":670,"y":1360,"wires":[]},{"id":"8fdd9c5e.7f901","type":"comment","z":"5b3a5364.173cdc","name":"BEDROOM TV AUTOMATION","info":"","x":120,"y":1060,"wires":[]},{"id":"a05c6337.cdd3d","type":"comment","z":"5b3a5364.173cdc","name":"TV AUTOMATION","info":"","x":130,"y":1120,"wires":[]},{"id":"bea4c6e9.680708","type":"server-state-changed","z":"5b3a5364.173cdc","name":"TV off?","server":"2fba4297.e4145e","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"media_player.philips_tv","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"off, unavailable","halt_if_type":"str","halt_if_compare":"does_not_include","outputs":2,"output_only_on_state_change":true,"x":90,"y":1160,"wires":[["b798bf07.73ce"],["dc3e5802.114d08"]]},{"id":"b798bf07.73ce","type":"switch","z":"5b3a5364.173cdc","name":"old state?","property":"data.old_state.state","propertyType":"msg","rules":[{"t":"eq","v":"off","vt":"str"},{"t":"eq","v":"unavailable","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":280,"y":1160,"wires":[["7455f425.27efbc","eaa9410.2c9efc"],["7455f425.27efbc","eaa9410.2c9efc"],[]]},{"id":"dc3e5802.114d08","type":"switch","z":"5b3a5364.173cdc","name":"old state?","property":"data.old_state.state","propertyType":"msg","rules":[{"t":"eq","v":"off","vt":"str"},{"t":"eq","v":"unavailable","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":280,"y":1400,"wires":[[],[],["c405973c.6ce688"]]},{"id":"2fba4297.e4145e","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

Hi, bumping in to my own question, that was long ago :-). I migrated to another media player and that follow a little different way of pausing/playing (sonos).

I am almost there I think but I cannot get the restoring of the volume level going.

Can you help out with what the error is and how I can solve that?

image

The code (yours):

newmsg = {}

newmsg.payload = { data: { "volume_level":msg.volume} }

return newmsg;

The error:

Property 'payload' is missing in type '{}' but required in type 'typeof newmsg'.(2741)

94(3, 1): 'payload' is declared here.

Some additional info… my mind is blowing… This is my test flow:
[{"id":"f0725b90.c2b32","type":"api-call-service","z":"6f59380a.4fad78","g":"26b5fa2a69eeaf22","name":"","server":"c6b350c5.b8b3c","version":5,"debugenabled":false,"domain":"media_player","service":"media_pause","areaId":[],"deviceId":[],"entityId":["media_player.woonkamer"],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1421,"y":327,"wires":[["165c63f347c29d6e","c5abbcdf15b51025"]]},{"id":"165c63f347c29d6e","type":"delay","z":"6f59380a.4fad78","g":"26b5fa2a69eeaf22","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"outputs":1,"x":1680,"y":328,"wires":[["45dda31f.44616c"]]},{"id":"45dda31f.44616c","type":"function","z":"6f59380a.4fad78","g":"26b5fa2a69eeaf22","name":"Save Settings","func":"msg.volume = msg.data.attributes.volume_level\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":1900,"y":320,"wires":[["4448c76f71ea4e52"]]},{"id":"4448c76f71ea4e52","type":"api-call-service","z":"6f59380a.4fad78","g":"26b5fa2a69eeaf22","name":"","server":"c6b350c5.b8b3c","version":5,"debugenabled":false,"domain":"media_player","service":"volume_set","areaId":[],"deviceId":[],"entityId":["media_player.woonkamer"],"data":"{\"volume_level\":0.5}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2170,"y":320,"wires":[["a381e862595ebb7b"]]},{"id":"a381e862595ebb7b","type":"api-call-service","z":"6f59380a.4fad78","g":"26b5fa2a69eeaf22","name":"","server":"c6b350c5.b8b3c","version":5,"debugenabled":false,"domain":"tts","service":"speak","areaId":[],"deviceId":[],"entityId":["tts.piper"],"data":"{\"cache\":true,\"media_player_entity_id\":\"media_player.woonkamer\",\"message\":\"Ding Dong, er wordt aangebeld. Er staat iemand voor de deur\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2380,"y":327,"wires":[["4e3923e37204b0e2"]]},{"id":"4e3923e37204b0e2","type":"delay","z":"6f59380a.4fad78","g":"26b5fa2a69eeaf22","name":"","pauseType":"delay","timeout":"4","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":2800,"y":320,"wires":[["491e7ba6.c0ae34"]]},{"id":"491e7ba6.c0ae34","type":"function","z":"6f59380a.4fad78","name":"Previous time","func":"newmsg = {}\n\nnewmsg.payload = { data: { \"volume_level\":msg.volume} }\n\nreturn newmsg;","outputs":1,"noerr":0,"x":3040,"y":340,"wires":[["a779d18.22da3b"]]},{"id":"a779d18.22da3b","type":"api-call-service","z":"6f59380a.4fad78","g":"26b5fa2a69eeaf22","name":"","server":"c6b350c5.b8b3c","version":5,"debugenabled":false,"domain":"media_player","service":"volume_set","areaId":[],"deviceId":[],"entityId":["media_player.woonkamer"],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":3296,"y":327,"wires":[[]]},{"id":"c6b350c5.b8b3c","type":"server","name":"Home Assistant","version":5,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

That flow gives no error but does not work. When I change the name of the node Previous time I get an error:
image

image

let newmsg = {}

newmsg.payload = { data: { "volume_level":msg.volume} }

return newmsg;
1 Like

Thanks! wouldn’t have known where to find this, it works well now, Thank you Mauricio!