Problem with creating a JSON-like string message in notify automation

Ok, then I get it.
EDIT: And knowing this will save me a lot of time in the future!

Yeah, it’s a pita sometimes. TBH an issue should be brought up about dictionaries in persistent_notification messages

Mm, weird. because when I checked it in the template dev tool it was a string. Like, when I tried an invalid operation on it the error was that the operation wasn’t valid on a str. So I assumed it was a string.

But I do notice since I think 2021.3 the UI yaml editor is a pain to use with Jinja :confused:

Sorry for digging out this old thread, but I’m trying to convert above script to new notify “send_message” format with no luck… Any help appriaciated, thanks!

I recommend you create a new post and provide details in the troubles you’re running into.

Sorry Petro, to explain myself: I’m the author of this topic, and I’ve been using above Tomas’ script for 3 years till now.
I’m going to update HA to 2024.12 and there was some changes in Notify service, so I got warning:

The File notify.zapisz_energia_dzienna action is migrated, but it seems the old notify action is still being used.

A new notify entity is available now to replace each legacy notify action.
Update any automations or scripts to use the new notify.send_message action exposed with this new entity. When this is done, select Submit and restart Home Assistant.

In other automation scripts in yaml this is quite easy to get done, but I’ve got this single python script, and have no idea how to change it to work after upgrade…

energy = hass.states.get("sensor.daily_energy").state
msg = {"date": 2021, "energy": float(energy)}
hass.services.call("notify", "send_message", service_data={"entity_id": ["<YOURENTITYIDHERE>"], "message": str(msg).replace("'",'"')})

Thank You very much, it seems to be working now:

hass.services.call("notify", "send_message", service_data={"entity_id": ["notify.zapisz_energia_dzienna"], "message": str(msg).replace("'",'"')})