Newbie needs help

hi im trying to make appdaemon push message to switch
rgb leds tru mqtt
random numbers are effect_r - solved
but i cant find howt to push it to broker - solved
for now i have

INFO AppDaemon: MQTT: Message Received: Topic = lights/ws2801/flipstrip1/in, Payload = b'{ effect: effect_r }' 

there must be number instead of effect_r
and that must work in cycle every 1 minute - still searching for answer
im not familiar with programming
and glad to recive some help.

import appdaemon.plugins.hass.hassapi as hass
import appdaemon.plugins.mqtt.mqttapi as mqtt
import datetime
import json

class HelloWorld(mqtt.Mqtt, hass.Hass):

    def initialize(self):
      self.set_namespace('mqtt')
      self.run_minutely(self.do_it(), datetime.time(0, 0, 0))

    def do_it(self):
      import random
      effect_r = random.randint(1, 40)
      self.mqtt_publish("lights/ws2801/flipstrip1/in", "{ effect: effect_r }", qos = 0, retain = True)
      self.log(effect_r)

I see two problems there - for valid json effect needs to be in quotes and effect_r needs to be replaced by its value. Try this

 self.mqtt_publish("lights/ws2801/flipstrip1/in", '{ "effect" : ' + str(effect_r) + '}', qos = 0, retain = True)
1 Like

that helps for pushing to mqtt. but still problem with making run repeating every minute.

i found that must be

self.run_minutely(self.do_it, datetime.time(0, 0, 0))

instead of
self.run_minutely(self.do_it(), datetime.time(0, 0, 0))
but now im get

2019-12-31 18:11:00.002343 WARNING AppDaemon: ------------------------------------------------------------
2019-12-31 18:11:00.002881 WARNING AppDaemon: Unexpected error in worker for App hello_world:
2019-12-31 18:11:00.003405 WARNING AppDaemon: Worker Ags: {'name': 'hello_world', 'id': UUID('aedd4cd4-08a7-4c05-b69e-cc1396c9b033'), 'type': 'timer', 'function': <bound method HelloWorld.do_it of <hello.HelloWorld object at 0xb4415230>>, 'kwargs': {'interval': 60}}
2019-12-31 18:11:00.004020 WARNING AppDaemon: ------------------------------------------------------------
2019-12-31 18:11:00.004910 WARNING AppDaemon: Traceback (most recent call last):
  File "/usr/lib/python3.7/site-packages/appdaemon/appdaemon.py", line 586, in worker
    funcref(self.sanitize_timer_kwargs(app, args["kwargs"]))
TypeError: do_it() takes 1 positional argument but 2 were given

The documentation for run_in is here. At the bottom of the section, it links to the callback definition, which should be the signature of your callback do_it

def do_it(self, kwargs):
2 Likes

FINAL AND FULLY WORKING VERSION HERE

import appdaemon.plugins.hass.hassapi as hass
import appdaemon.plugins.mqtt.mqttapi as mqtt
import datetime
import json

class HelloWorld(mqtt.Mqtt, hass.Hass):

    
    def initialize(self):
      self.set_namespace('mqtt')
      self.run_minutely(self.do_it, datetime.time(0, 0, 0))
      self.log("Hello from flipsteram random numbers controller")
    def do_it(self, kwargs):
      import random
      effect_r = random.randint(1, 40)
      self.mqtt_publish("lights/ws2801/flipstrip1/in", '{ "effect" : ' + str(effect_r) + '}', qos = 0, retain = True)
      self.log(effect_r)

thx u help me a lot , maybe i need to get some object oriented programming course next year )
happy new year and lot of luck and happines for you and WHOLE COMMUNITY

2 Likes