Assuming that the problem is the fancy single quotes and that this is not just a cut/paste formatting error* you need to replace them with double quotes, then it is a valid JSON string according to an on-line parser I tried:
This template sensor will do that, but you have not given anywhere near the amount of information required to complete this fully so you will have to fill in the blanks.
a_sensor_containing_a_json_attribute: # the name you want to call the new sensor, likewise with the friendly name
sensor.where_the_value_comes_from # the sensor you want to add the attribute to. You’re actually making a new sensor. You can’t add an attribute to the existing one.
the_jason_string # replace with the name you want to call the attribute.
sensor.original_sensor_object_id # the sensor that contains the JSON string you want to add.
*If the fancy single quotes are due to a cut/paste formatting error you need to learn how to format your post correctly. See point 11 here: How to help us help you - or How to ask a good question In which case the attribute template becomes:
Hi, you were correct the first time. it is a single quotes that came from the device I reading.
I understood what you wrote and heading to try them right away.
One more question. the data is there in a very specific time… that means I nead the parsing to happen as an automation. otherwise everytime the data change in the source it will change in the template sensor as well.
How can I do that in an automation?
what’s making the sensor? I’d like to see that yaml. I have solutions, but until I see that intial setup, I won’t be able to make the simplest solution for you.
Can do that in automation? the data is there in a very specific time… that means I nead the parsing to happen as an automation. otherwise everytime the data change in the source it will change in the template sensor as well.
There are more than one attribute in the data. how can I iterate on all the string?
And then is that MQTT topic Auto discovered? I feel like you aren’t reading my response and giving the least amount of information.
Do you have a configuration for this device?
Is it discovered?
Seeing that these 2 questions somehow escape your mind, I will offer a solution that I think will work.
Configure (Not Discover) the mqtt sensor. Use this configuration for the sensor. BTW, this is literally the example in the documentation doing exactly what you want.
- platform: mqtt
name: "Timer 1"
state_topic: "tele/sonoff/sensor"
value_template: "{{ value_json.Timer1.Arm }}"
json_attributes_topic: "tele/sonoff/sensor" #< ----- CHANGE THIS TO YOUR DEVICE TOPIC.
json_attributes_template: "{{ value_json.Timer1 | tojson }}"
It will create a sensor: sensor.timer_1 that will contain the state equal to the arm value. and the attributes will be: arm, mode, time, window, days, repeat, output, and action.
So if you want to create any separate sensor with that information, you’ll make a template sensor using the following code, but changing the attribute.
There is no way to pull the timer data directly. It is not posted in any topic as default so I can pull it.
The only way is to post a message to ‘cmnd/timer1’, and the data is then posted in the topic ‘stat/results’.
Now, I cant put a constant sensor on the ‘stat/results’ topic, since the result of every command is posted there. So I need to time this… send the command, and only then go and collect the data.
Now, I tried what you suggested before, but I got an error in the log saying it is not a JSON data, and now I know why.
So the task is like this:
Trigger the ‘cmnd/timer1’ command every 15 minutes (Done)
collect the data from ‘stat/results’ (Done)
replace the quotes
put the new data as a JSON template with attribute
I hope I managed to give all the info I have. sorry…
and thanks again for your patience.
BTW, the example from the documentation is wrong. there is no topic ‘tele/sonoff/sensor’… so the data cannot be pulled as explained above
You have to change it to whatever topic your results are posted from. It’s not a copy paste example. It’s showing you what it could look like.
Notice how the comment says:
Humor me and please take a screenshot of the developer tools → template page with the following code: CHANGE THE ENTITY_ID IN THE CODE TO YOUR stat/results ENTITY. Also make sure you push the correct result to the sensor before taking the screenshot.
The problem is that the info on that sensor can change because every time I issue a command the result is posted there.
Thats why I wanted to do it in automation, so I collect the data only after yriggering the result I need
Please read the link I posted on formatting your code properly. You need to use code blocks not quote. It is messing up your quotation marks and leads to confusion.