MQTT sensors and switches - status issues

Hi,

I have a MQTT file with two sensors and a number of switches. I am facing two issues:

Sensors:

These are both intended to get temperature from a couple of devices. The first one works and the other one shows unavailable. They both have the same code and I am using a state topic and expire_after: 2629746. The first works, the second is unavailable. If I remove the expire_after it shows as unknown instead but the icon changes from grey to blue.

I am using json_value.value for both but have tried unique terms instead of value and get the same result.

Switches:

I get lightning bolts - turning the light switch on manually or using the lightning bolt updates it to a toggle switch and it shows the status. On startup it does not show the status.

Are there any tips or tricks to get this working?

I’m not sue how to copy code into here without formatting but can do so if someone can tell me how.

Hello Deano,

It would help if you could to provide some code so we can see what you’ve done so far… We won’t be able to help if all we have is ‘I want to do this thing with this other thing’.
It makes it much easier for us to offer constructive answers if you follow Community Guidelines #9. 11, & 14 by supplying a properly formatted configuration for the automation that you have attempted. Please share the YAML and any errors so we can see what you’ve done.

I tried that but even in the code bit there was loads of formatting that you get for text (like %20, etc). I’ve copied it into something else and then to here and it should be ok.


sensor:
  - name: "AJAX Outside Temperature"
    state_topic: "jeedom/cmd/event/20"
    unique_id: "temperature_outside_alarm_siren"
    state_class: measurement
    unit_of_measurement: "°C"
    value_template: '{% if value_json.value is defined and value_json.value != "" %}{{value_json.value}}{% else %}{{this.state}}{% endif %}'
    expire_after: 2629746
    device:
      identifiers: AJAX Alarm via Jeedom MQTT
      name: AJAX Alarm
      model: Hub 2 4G
      manufacturer: AJAX

  - name: "AJAX Upstairs Temperature"
    state_topic: "jeedom/cmd/event/26"
    unique_id: "temperature_upstairs_fire_alarm"
    state_class: measurement
    unit_of_measurement: "°C"
    value_template: '{% if value_json.value is defined and value_json.value != "" %}{{value_json.value}}{% else %}{{this.state}}{% endif %}'
    expire_after: 2629746
    device:
      identifiers: AJAX Alarm via Jeedom MQTT
      name: AJAX Alarm
      model: Hub 2 4G
      manufacturer: AJAX

This is the current state of the code - prior to this the value_json was .value in both but I thought I’d try something different.

I get temperature for the first but status is unavailable for the second. If I remote the expire_after from the second one then the icon goes from grey to blue but status is unknown.

Switches are as follow (but more of them):


switch:
  - name: "Kitchen Lights"
    unique_id: "new_kitchen_light_switch"
    state_topic: "jeedom/cmd/event/134"
    command_topic: "jeedom/cmd/set"
    value_template: '{{value_json.value}}'
    state_on: "1"
    state_off: "0"
    payload_on: '{ "56" : "SWITCH_ON" }'
    payload_off: '{ "57" : "SWITCH_OFF" }'
    retain: false
    optimistic: false
    device:
      identifiers: AJAX Alarm via Jeedom MQTT
      name: AJAX Alarm
      model: Hub 2 4G
      manufacturer: AJAX

  - name: "Patio Lights"
    unique_id: "patio_lights_switch"
    state_topic: "jeedom/cmd/event/48"
    command_topic: "jeedom/cmd/set"
    value_template: '{{value_json.value}}'
    state_on: "1"
    state_off: "0"
    payload_on: '{ "52" : "SWITCH_ON" }'
    payload_off: '{ "53" : "SWITCH_OFF" }'
    retain: true
    optimistic: false
    device:
      identifiers: "AJAX Alarm via Jeedom MQTT"
      name: "AJAX Alarm"
      model: "Hub 2 4G"
      manufacturer: "AJAX"

I have the lightning symbols for each switch. If I turn on the light manually or by clicking the lightning symbol then the status updates (the switches do turn on and off - it’s just the status only updates after the status first changes and after that they are ok until HA is restarted.

Data is coming from the Jeedom Ajax plugin and I believe retain flags are set. It’s certainly the case that the first temperature sensor is always immediately available after a reset.

I don’t see any big problems. I would make sure the retain is on on the switches. One looks on there one is off. The lightning bolt means that HA doesn’t have info if it’s on or off so load up MQTT explorer and watch the numbers in the broker so you know what is happening there.
MQTT-Explorer If you have Add-ons available, try adding this Add-on repository to your Add-on list. GitHub - GollumDom/addon-repository.
With this you can see that the broker sees and likely find the bug.
HINT: I have found using this, if you look at the JSON string and it is in color, the JSON is valid. If it is monochrome, the JSON is invalid and it will not work.

The retain flags were different because I was trying different things to see if they made any difference.

Thanks for the link to the MQTT-Explorer.

I restarted HA to get a fresh look at what is happening and of course the upstairs temperature suddenly showed up for the first time… no idea why.

All switches are oblivious to the current status.

I restarted HA again and both temperatures still appeared, which is good but odd. In MQTT explorer I do not see any data relating to the reported temperatures though - I assume I should see those values there somewhere?

MQTT Explorer:


▼jeedom
state = online
▼ajax
▼1
▼11
set = SWITCH_OFF
cmd = SWITCH_OFF
▼Home_Hub
▼LightDRKitchen
set = SWITCH_OFF
cmd = SWITCH_OFF
▼003185EB
▼301C1D44
set = SWITCH_OFF
cmd = SWITCH_OFF
▼cmd
▼set = { "53" : "SWITCH_OFF" }
11 = SWITCH_OFF
= 56
cmd = 56
event = 56
▼
▼jeedom
▼cmd
▼set = 56
= 56

Only one light is currently on and that’s not visible in the above. ‘56’ relates to the switch_on command for the kitchen light. ‘53’ is patio lights off, which they are.

If I turn a light on manually then I immediately get full information and it immediately updates when I turn it off:


48 = {"value":0,"humanName":"[Living Room][LightLROutside][State]","unite":"","name":"State","type":"info","subtype":"binary"}
58 = {"value":0,"humanName":"[Office][LightOfficeOutside][State]","unite":"","name":"State","type":"info","subtype":"binary"}
134 = {"value":0,"humanName":"[Kitchen][LightDRKitchen][State]","unite":"","name":"State","type":"info","subtype":"binary"}

Anything I click on shows in the right that it is a retained value. If I click on the first of the three items above I get this in the right:


 
 
 
 
 
 
 
{
  "value": 1,
  "value": 0,
  "humanName": "[Living Room][LightLROutside][State]",
  "unite": "",
  "name": "State",
  "type": "info",
  "subtype": "binary"
}

I think Jeedom has a retained command in all Ajax items but it’s a mess compared to HA and not particularly clear.