Now I got it… fixed the post.
Automations can’t populate values into devices.
I find it odd that you are the only person with sonoff that has this setup. Why can’t you change the topic for your results? Everything going to 1 topic is going to make this painful for you.
value_template: "{{ value_json.replace'\'',"\"" }}"
syntaix is wrong
value_template: '{{ value.replace("\'","\"") }}'
Also, this will be alot easier if you can get this into json_attributes_template because then it will be a dictionary and you can just request the values.
json_attributes_template: '{{ value.replace("\'","\"") }}'
As for peeling the info out, you’d still need to create template sensors.
- platform: template
sensors:
time_1_mode:
friendly_name: Timer 1 Mode
value_template: >
{% set value = states.sensor.timer1.attributes %}
{% if states.sensor.timer1.attributes.Timer1 is defined %}
{{ states.sensor.timer1.attributes.Timer1.Mode }}
{% else %}
{{ states('sensor.timer_1_mode') }}
{% endif %}
Where did “sensor.boiler_mqtt_result” come from? did you create that sensor in configuration.yaml (or it’s !ncludes) or did it get automatically created by discovery of your sonoff?
Which sonoff device are you using? Which firmware are you using?
Do you have an mqtt network sniffer to be able to see the MQTT traffic on your network? If not, then install and configure MQTTFx or something similar. Subscribe to all topics the when you publish the command see what the topic is that gets returned.
If you use tasmota or some other popular off the shelf firmware I would be surprised to see it post to such a generic topic unless you set it up that way. Normally the default topics are fairly device specific.
Yah exactly. This setup seems needlessly complicated
@petro, @finity, @123 and @tom_l
Hi, guys. First I want to thank all of you guys for taking the time to try and help me.
I spent the last two days exploring and testing, to make sure I do it right. and I can divide my problem to two issues:
- The quotes and double quotes issue
- The publish and collect issue
About the first issue, I found what is causing the data to show in single quotes. it is because I use the following line in the sensor config:
value_template: "{{ value_json }}"
Once I removed that line, the data collected was in a correct JSON syntax using the double quotes.
Now that it is fixed, the code given above create the attributes correctly.
{"Timer1":{"Arm":0,"Mode":0,"Time":"07:00","Window":0,"Days":"1111111","Repeat":1,"Output":1,"Action":1}}
About the second issue:
The device is a Tuya switch with Tasmota installed (version 6.5.0(release-basic)). It is configured and not discovered with the following config (with the latest addition):
- platform: mqtt
name: "boiler_mqtt_result"
state_topic: "stat/boiler/RESULT"
json_attributes_topic: "stat/boiler/RESULT"
json_attributes_template: "{{ value_json.Timer1 | tojson }}"
The problem is that the Timer data is not presented on any topic so I can just put a constant sensor on it.
The only way to have it is to send the command "time1 "to the cmnd/boiler/ topic.
Than the timer1 data are sent back to the general topic “stat/boiler/RESULT” as a response like every Tasmota command.
e.g If I send the command “on” to “cmnd/boiler/power1”, the configuration is also sent to “stat/boiler/RESULT”.
And this is only for timer1… If I have more than one timer, I need to do that for each one.
It is not something I made difficult, that’s how it works (as far as I know)
Now the data is populated as attributes as it should, and automation is running every 15 minutes triggering the timers data to appear in that topic.
The only thing left is to copy the data to another entity with the same attributes. I assume a template sensor will be the right entity to do so.
Another problem is that the code from the example can only handle Timer1 data (it is fixed in the config). Every time I trigger the Timer2 data, I see an error in the log. Can I use a template to address both?
Again, thanks for your patience and your will to help.
Naor.
Which module did you configure the tuya switch as in the Tasmota console?
What is the error in the log when you try to trigger the timer2 result?
Change that to this
- platform: mqtt
name: "boiler_mqtt_result"
state_topic: "stat/boiler/RESULT"
json_attributes_topic: "stat/boiler/RESULT"
json_attributes_template: "{{ value_json | tojson }}"
and any timer will work.
The attributes in that result will change though. This template sensor should peel out the correct timer data
- platform: template
sensors:
time_1_mode:
friendly_name: Timer 1 Mode
value_template: >
{% set value = states.sensor.boiler_mqtt_result.attributes %}
{% if value.Timer1 is defined %}
{{ value.Timer1.Mode }}
{% else %}
{{ states('sensor.timer_1_mode') }}
{% endif %}
Copy and adjust the template sensor multiple times to get the results you desire. I.E swap Timer1 for Timer2 etc.
@finity, the module is Zemismart WH (0). I bought it with tasmota already installed with that module.
@petro, your logic is finally catching me… no automation needed for populating the data only to trigger the data to appear, once the first sensor have the relevant data, the template sensor will update in real time.
I need the Arm and Time data from it. Can I iterate in the template and have one template sensor with attributes for each timer? or I need to have a template sensor for each attribute?
Will something like this work?
- platform: template
sensors:
timer_1:
friendly_name: Timer 1
value_template: >
{% set value = states.sensor.boiler_mqtt_result.attributes %}
{% if value.Timer1 is defined %}
{{ value.Timer1 }}
{% else %}
{{ states('sensor.timer_1') }}
{% endif %}
attribute_templates:
arm: >
{% set value = states.sensor.boiler_mqtt_result.attributes %}
{% if value.Timer1 is defined %}
{{ value.Timer1.Arm }}
{% else %}
{{ states('sensor.timer_1') }}
{% endif %}
time: >
{% set value = states.sensor.boiler_mqtt_result.attributes %}
{% if value.Timer1 is defined %}
{{ value.Timer1.Time }}
{% else %}
{{ states('sensor.timer_1') }}
{% endif %}
Update
Tried it and got the following log error:
[attribute_templates] is an invalid option for [sensor.template]. Check: sensor.template->sensors->timer_1->attribute_templates.
But the documentation have that option… here
Another thing, every time I trigger the other timer2 data, all the info of timer1 data become unknown
until I flip it back again (and than that happen to the timer2 data)
No you have to have separate templates.
Please post all your templates, when timer2 or timer1 get populated, it should not affect the other.
Works like a charm… thank you guys very much! Not only I can present the data, my knoledge in templating in better now.
Thanks you helping.
boiler_timer1_arm:
friendly_name: boiler timer 1 arm
value_template: >
{% set value = states.sensor.boiler_mqtt_result.attributes %}
{% if value.Timer1 is defined %}
{{ value.Timer1.Arm }}
{% else %}
{{ states('sensor.boiler_timer1_arm') }}
{% endif %}
boiler_timer1_time:
friendly_name: boiler timer 1 time
value_template: >
{% set value = states.sensor.boiler_mqtt_result.attributes %}
{% if value.Timer1 is defined %}
{{ value.Timer1.Time }}
{% else %}
{{ states('sensor.boiler_timer1_time') }}
{% endif %}
boiler_timer2_arm:
friendly_name: boiler timer 2 arm
value_template: >
{% set value = states.sensor.boiler_mqtt_result.attributes %}
{% if value.Timer2 is defined %}
{{ value.Timer2.Arm }}
{% else %}
{{ states('sensor.boiler_timer2_arm') }}
{% endif %}
boiler_timer2_time:
friendly_name: boiler timer 2 time
value_template: >
{% set value = states.sensor.boiler_mqtt_result.attributes %}
{% if value.Timer2 is defined %}
{{ value.Timer2.Time }}
{% else %}
{{ states('sensor.boiler_timer2_time') }}
{% endif %}