"mesage":"invalid JSON specified"

So i have a python script that i run on a remote raspberry pi with a pir hc s501 motion sensor.
i have a binary sensor setup with platform mqtt, but not sure what to use as a state_topic,

 binary_sensor:
   - platform: mqtt
     name: "MotionSensor"
     state_topic: "movement/motionsensor/motion"

my python script checks the state of a gpio pin, then based of an if statement sends a http request to the resful api like this:

 import requests
 
 url = 'http://ipaddress:8123/api/states/binary_sensor.motionsensor'
 headers = {'x-ha-access': 'password', 'content-type': 'application/json'}
 data = {"state": "on", "attributes": {"friendly_name": "motionsensor"}}
 response = requests.post(url, headers=headers, data=data)

im receiving an error back from home assistant:

{ā€œmessageā€:ā€œinvalid JSON specifiedā€}

anyone ever run into this issue before and how to resolve it?

First what does this have to do with Zwave config?

Why do you think that subscribing to a MQTT topic will allow a rest request to populate an MQTT service?

Your remote PI should publish to a MQTT broker on the state_topic defined above and that’s assuming you have a broker setup in the first place tho’

well i have an mqtt broker running for several cameras with motion using motioneyeos, and a curl command with the json object seems to work fine with it. but i understand what youre saying i should probably use mosquitto publish from the remote pi to the home assistant server. ill give that a try…thanks! i am new to this(insert excuse here) just started back in jan. and im not that familiar with mqtt.

:slight_smile: no problem tho you should move this out of zwave config as it’s not related at all, probably better off in normal config :slight_smile:

MQTT seems to confuse a lot of peeps (me to in the early days if I’m honest), but it’s really simple. Think of the mqtt broker as a message board that anyone can send a message to and can pick up a message from provided they know the message route, where the message route is the topic. So one side publishes a message to a topic and the other side that wants to receive it subscribes to that topic, that’s it really, simple :slight_smile:

Edit: moved to config

yup, i think my confusion was based off how i was applying notification commands to the mqtt broker via curl and the restful api. maybe setting up an http binary sensor for the zwave would work as well.

What zwave?

This one:
HC S501

That’s not a zwave device, its a PIR sensor (passive infrared sensor), nothing what so ever to do with zwave :stuck_out_tongue:

lol true, i also have a zwave device monoprice with motion temperature battery and humidity, just no hub yet. and i have no idea how to wire the damn thing to my raspberry pi(so far)…i purchased a ha1000, but that was a waste, they had been discontinued (staples connect) and the device was already registered. Then i tried the uboot exploit to drop into a shell so i could control it since i kept getting a 503.x certificate error when i would try to access the http user interface. but i think its bricked lol.

You don’t ā€œwireā€ it to your pi you need a usb zwave stick to control it and any other zwave devices you may have, you can get them very cheap these days. Mine cost about Ā£35 when I bought it about 1.5 yrs ago, but I just saw this …
https://www.ebay.co.uk/itm/Everspring-SA413-Z-Wave-Plus-USB-Adapter-Stick/282638002736?epid=2222971112&hash=item41ce89aa30:g:T0gAAOSwcndZrS11

How good it is I can’t say but for 5 quid :stuck_out_tongue:

I might get one as a spare :slight_smile:

1 Like

yeah i know i need a z stick but i was trying to make it a little cheaper, then just 3d print a case for the raspberry pi and the zwave device.i mean i should be able to wire it to the gpio’s some how straight off the zwave board, theres several holes in it for that purpose i believe, just need to find the schematics.

Good luck with that, I wouldn’t even try for the cost of a zwave stick.

If your still talking about the PIR yes you can wire that directly it has a very simply I/F just 5v, gnd and signal.

no im talking about the zwave, i already have the pir sensor working just now trying to finish up the script using paho-mqtt for python, im gonna have that script run on boot, then when it picks up motion it will send a connect to the broker and publish to the topic, then go back to standby after 5 seconds, once i figure out how to place the password in the connect parameters with paho-mqtt i should be good to go.

1 Like

lol so youre gonna find this hilarious!!

import requests
url = ā€˜http://ipaddress:8123/api/states/binary_sensor.motionsensor’
headers = {ā€˜x-ha-access’: ā€˜password’, ā€˜content-type’: ā€˜application/json’}
data = ā€˜{ā€œstateā€: ā€œonā€, ā€œattributesā€: {ā€œfriendly_nameā€: ā€œmotionsensorā€}}’
response = requests.post(url, headers=headers, data=data)

all i had to do was add quotes to the json dictionary and it works fine LOLOLOLOL.
compare data above to my data below, just had to put in quotes.
configuration.yaml:

binary_sensor:

  • platform: mqtt
    name: ā€œMotionSensorā€
    state_topic: ā€œmovement/motionsensor/motionā€

python script:

import RPi.GPIO as GPIO
import time
import requests

GPIO.setmode(GPIO.BCM)

GPIO.setup(pinnumber, GPIO.IN) #PIR
#GPIO.setup(24, GPIO.OUT) #BUzzer
url = 'http://ipaddress:8123/api/states/binary_sensor.motionsensor'
headers = {'x-ha-access': 'password', 'content-type': 'application/json'}
data = '{"state": "on", "attributes": {"friendly_name": "MotionSensor"}}'

url1 = 'http://ipaddress:8123/api/states/binary_sensor.motionsensor'
headers1 = {'x-ha-access': 'password', 'content-type': 'application/json'}
data1 = '{"state": "off", "attributes": {"friendly_name": "MotionSensor"}}'

try:
    time.sleep(2) # to stabilize sensor
    while True:
        if GPIO.input(pinnumber):
            #GPIO.output(24, True)
            #time.sleep(0.5) #Buzzer turns on for 0.5 sec
            #GPIO.output(24, False)
            #print("Motion Detected...")
            #localtime = time.asctime( time.localtime(time.time()) )
            #print(localtime)
            response = requests.post(url, headers=headers, data=data)
            #print(response.text)
            time.sleep(5) #to avoid multiple detection
            response1 = requests.post(url1, headers=headers1, data=data1)
            #print(response1.text)

        time.sleep(0.1) #loop delay, should be less than detection delay

except:
    GPIO.cleanup()

Well it’s funny in a strange way as this still has nothing to do with MQTT or zwave, what you are doing is calling in to the HA api to set the state of a variable, that variable could just as well be a standard binary sensor or just a sensor, it has nothing to do with mqtt. However congrats on solving it :smiley:

youre right!!, i got confused with my zwave sensor and my pir sensor. but once i learn how to wire the zwave device to the raspberry pi… ill let you know, so if someone wants to bypass purchasing a zstick or hub and run straight from the gpio, youll just need a raspberry pi and any zwave device(and a 3d printer for a case), i mean a raspberry pi zero w is only 15 bux and a raspberry pi is like 35 bux, where as z stick runs around 40 - 50, and if you just have 1 zwave sensor, youre good to go.

Have a look at the link I posted above, the zwave stick can be had for Ā£5, that’s a UK version, for the US you would need a different one but the cost wouldn’t be much more than $7 i would think :slight_smile:

i cant find any zsticks for less than 44 bux lol