I’ve got a MQTT sensor that is throwing errors because sometimes the payload doesn’t contain the parameters that I’m pulling out.
The sensors are working because it eventually does contain them, but I’d like to not have my log spammed with errors. I do not have control over the source MQTT being published so I can’t modify it to split up the groupings under their own MQTT topics nor ensure that all of them exist in each publishing.
What I’d like to do is if the attribute doesn’t exist in the JSON, just use the last known value (and ALSO don’t throw an error in my log).
Example of sensor:
- name: "Ecoflow Beeper"
icon: "mdi:volume-source"
state_topic: "bridge-ecoflow//app/device/property/DAABZ5ZE8020852"
payload_on: True
payload_off: False
value_template: >-
{% set value = value_json['params']['pd.beepState'] | int("invalid") %}
{% if value == "invalid" %}
{{ states("binary_sensor.ecoflow_beeper") }}
{% else %}
{# 1=Mute 0=Sound #}
{{ value == 0 }}
{% endif %}
Example of errors:
2022-12-18 11:11:36.573 ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: 'dict object' has no attribute 'pd.beepState' (value: {"params":{"inv.dcInVol":0,"inv.cfgAcWorkMode":0,"inv.cfgAcOutVoltage":120000,"inv.cfgStandbyMin":0,"inv.dcInAmp":0,"inv.cfgAcOutFreq":2,"inv.errCode":0,"inv.outputWatts":0,"inv.dcInTemp":26,"inv.invOutFreq":0,"inv.cfgFastChgWatts":1800,"inv.chargerType":0,"inv.acInAmp":0,"inv.fanState":0,"latestTimeStamp":1671379896479,"inv.cfgAcEnabled":0,"inv.cfgAcXboost":0,"inv.outTemp":25,"inv.invType":0,"inv.acDipSwitch":2,"inv.invOutVol":0,"inv.acInVol":0,"inv.inputWatts":0,"inv.acPassByAutoEn":0,"inv.acInFreq":0,"inv.chgPauseFlag":0,"inv.dischargeType":0,"inv.invOutAmp":0,"inv.sysVer":16843552,"inv.cfgSlowChgWatts":400},"version":"1.0","cmdId":0,"cmdFunc":0,"id":1345560820876055542,"addr":0,"timestamp":1671379896479}, template: {% set value = value_json['params']['pd.beepState'] | int("invalid") %} {% if value == "invalid" %}
{{ states("binary_sensor.ecoflow_beeper") }}
{% else %}
{# 1=Mute 0=Sound #}
{{ value == 0 }}
{% endif %})
Example data:
{
"params": {
"inv.dcInVol": 0,
"inv.cfgAcWorkMode": 0,
"inv.cfgAcOutVoltage": 120000,
"inv.cfgStandbyMin": 0,
"inv.dcInAmp": 0,
"inv.cfgAcOutFreq": 2,
"inv.errCode": 0,
"inv.outputWatts": 0,
"inv.dcInTemp": 36,
"inv.invOutFreq": 0,
"inv.cfgFastChgWatts": 1800,
"inv.chargerType": 1,
"inv.acInAmp": 10505,
"inv.fanState": 0,
"latestTimeStamp": 1671242617303,
"inv.cfgAcEnabled": 0,
"inv.cfgAcXboost": 0,
"inv.outTemp": 39,
"inv.invType": 0,
"inv.acDipSwitch": 1,
"inv.invOutVol": 0,
"inv.acInVol": 111373,
"inv.inputWatts": 1109,
"inv.acPassByAutoEn": 0,
"inv.acInFreq": 60,
"inv.chgPauseFlag": 0,
"inv.dischargeType": 0,
"inv.invOutAmp": 0,
"inv.sysVer": 16843552,
"inv.cfgSlowChgWatts": 400
},
"version": "1.0",
"cmdId": 0,
"cmdFunc": 0,
"id": xxxxxxxxx,
"addr": 0,
"timestamp": 1671242617303
}
-then later-
{
"params": {
"pd.iconWifiMode": 0,
"pd.iconOverloadState": 0,
"pd.wattsInSum": 1127,
"pd.wifiVer": 0,
"pd.iconLowTempMode": 0,
"pd.iconGasGenMode": 0,
"pd.iconBmsParallelMode": 0,
"pd.iconInvParallelState": 0,
"pd.typec2Watts": 0,
"pd.iconTypecState": 0,
"pd.iconCarMode": 1,
"pd.iconCoGasState": 0,
"pd.iconRcState": 0,
"pd.iconHiTempState": 0,
"pd.iconHiTempMode": 0,
"pd.iconEcoMode": 0,
"pd.usb1Watts": 0,
"pd.standByMode": 0,
"latestTimeStamp": 1671242620559,
"pd.chgPowerDc": 408,
"pd.dsgPowerDc": 328,
"pd.iconUsbState": 0,
"pd.typccUsedTime": 4079,
"pd.iconBtMode": 0,
"pd.typec2Temp": 28,
"pd.carUsedTime": 4493,
"pd.typec1Watts": 0,
"pd.soc": 86,
"pd.iconFactoryState": 0,
"pd.iconChgStationState": 0,
"pd.iconBmsParallelState": 0,
"pd.wirelessWatts": 0,
"pd.iconCarState": 0,
"pd.iconSocUpsMode": 0,
"pd.iconSolarBracketMode": 0,
"pd.wattsOutSum": 0,
"pd.iconWifiState": 0,
"pd.iconWindGenMode": 0,
"pd.iconUsbMode": 0,
"pd.iconBmsErrState": 0,
"pd.iconBmsErrMode": 0,
"pd.iconWindGenState": 0,
"pd.iconBtState": 0,
"pd.iconEcoState": 0,
"pd.carState": 0,
"pd.invUsedTime": 169719,
"pd.iconAcFreqMode": 1,
"pd.typec1Temp": 27,
"pd.dsgPowerAc": 2693,
"pd.dcInUsedTime": 2570,
"pd.iconLowTempState": 0,
"pd.model": 1,
"pd.chgPowerAc": 5154,
"pd.beepState": 0,
"pd.iconPackHeaterState": 0,
"pd.wifiAutoRcvy": 0,
"pd.remainTime": 26,
"pd.iconFanState": 0,
"pd.chgSunPower": 0,
"pd.carTemp": 0,
"pd.lcdBrightness": 100,
"pd.iconRechgTimeState": 0,
"pd.qcUsb2Watts": 0,
"pd.lcdOffSec": 300,
"pd.iconTransSwState": 0,
"pd.iconTypecMode": 0,
"pd.sysVer": 16844309,
"pd.iconOverloadMode": 0,
"pd.iconRechgTimeMode": 0,
"pd.sysChgDsgState": 2,
"pd.iconInvParallelMode": 0,
"pd.qcUsb1Watts": 0,
"pd.iconTransSwMode": 0,
"pd.iconFactoryMode": 0,
"pd.kit0": 0,
"pd.kit1": 0,
"pd.kit2": 0,
"pd.iconFanMode": 1,
"pd.usbqcUsedTime": 4089,
"pd.dcOutState": 0,
"pd.usbUsedTime": 4197,
"pd.mpptUsedTime": 80,
"pd.iconSolarPanelState": 0,
"pd.iconWirelessChgMode": 0,
"pd.wifiRssi": 0,
"pd.iconSocUpsState": 0,
"pd.iconSolarBracketState": 0,
"pd.errCode": 0,
"pd.iconPackHeaterMode": 0,
"pd.iconCoGasMode": 0,
"pd.carWatts": 0,
"pd.iconChgStationMode": 0,
"pd.iconAcFreqState": 0,
"pd.iconGasGenState": 0,
"pd.usb2Watts": 0,
"pd.iconRcMode": 0,
"pd.iconWirelessChgState": 0,
"pd.iconSolarPanelMode": 0
},
"version": "1.0",
"cmdId": 0,
"cmdFunc": 0,
"id": xxxxxxxxxxxxxx,
"addr": 0,
"timestamp": 1671242620559
}